• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Melissa · Mar 26, 2010 at 05:30 PM · camera

Click 3D objects (markers) in the distance, causing the camera to snap to that location

I have seen a couple of similar questions asked, but none that quite get to what I'm trying to do. I am working on a project where I want the user to be able to click on various objects floating in the air in 3D space (in the distance). When the user clicks that object, I want the camera to snap to that position. I need to specify different locations for each 3D marker. Can anyone help me with this? I've added a link to a photo of my scene.

http://www.freeimagehosting.net/uploads/a4eca8ea6b.jpg

Thank you in advance for any help you can offer!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by duck · Mar 26, 2010 at 06:06 PM

something like this (placed on each marker) ought to do the job, in a very basic way:

function OnMouseUp() {
    Camera.main.transform.LookAt(transform.position);
    Camera.main.transform.position = transform.position;
}

The above simply turns the camera to point at the clicked marker, then sets its position to the marker's position.

You may want to transition the camera to the marker somehow, in which case you'd probably want to use a coroutine and the "Lerp" function, something like this:

function OnMouseUp() {

 var startTime = Time.time;
 var startPos = Camera.main.transform.position;
 var startRot = Camera.main.transform.rotation;
 var targetPos = transform.position;
 var targetRot = transform.rotation;

 targetPos.y = Camera.main.transform.position.y;  // preserve cam's height

 while (Time.time < startTime+1) {
     var i = (Time.time - startTime);
     Camera.main.transform.position = Vector3.Lerp(startPos, targetPos, i);
     Camera.main.transform.rotation= Quaternion.Slerp(startRot, targetRot, i);
     yield;
 }

}

(edited to preserve cam's height, as requested in the comments)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Melissa · Mar 27, 2010 at 12:48 AM 0
Share

This is great! $$anonymous$$y only problem is that I have the camera slightly elevated above the buildings, and when it jumps to the marker's location, it doesn't keep the marker's y-value. Then I find myself inside the building and my colliders don't seem to work anymore. Any suggestions? Thanks again!

avatar image KvanteTore · Mar 27, 2010 at 08:32 AM 0
Share

When you move to the position of the marker, you move your center to the center of the marker. Could it be that the markers have a center below their apparent center of the mesh? You could either move the markers higher, change the mesh of the marker so that its center is higher up, or add a y-value to the destination position you are lerping to.

avatar image Melissa · Mar 27, 2010 at 04:30 PM 0
Share

Thanks. I will check that stuff out. How do I specify a particular y-value?

avatar image duck ♦♦ · Mar 27, 2010 at 08:45 PM 0
Share

I've just edited the example to show how to preserve the camera's height. You could modify this line to specify any height for targetPos.y

avatar image Melissa · Mar 28, 2010 at 03:05 AM 0
Share

Great! Thank you SO $$anonymous$$UCH!

avatar image
0

Answer by Melissa · Mar 27, 2010 at 12:16 AM

I didn't mean to post in the answer section.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to make camera position relative to a specific target. 1 Answer

UI in multiplayer problem 2 Answers

Quaterion.Slerp + Lookat, camera flipping 0 Answers

Camera movement and gun follow in unity 1 Answer

Camera ClearFlags DepthOnly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges