Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Eowyn27 · Jan 06, 2014 at 08:47 PM · c#cameratransformobjectonmousedown

How do I move the camera to another object in the scene on mouse down?

I want to attach a script to my object that when I click on that object, the camera moves to another object.

So, currently my main camera is focused on one object but I want that camera to move when I click on a specific object and land on another object.

I was thinking since i have the coordinates of both objects, I can move the camera on mouse down from coordinate (x1, y1, z1) to (x2, y2, z2) but I'm not sure how or what function to use.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jinxology · Jan 06, 2014 at 09:49 PM

Add a script to your clickable objects that says:

 var mainCam:Camera;
 
 function OnMouseUp() {
    mainCam.transform.position = new Vector3(transform.position.x, transform.position.y, mainCam.transform.position.z); 
 }
 

Make sure to drag your main camera to the mainCam field in the inspector. I left the Z position on the camera the same since I figured your may want to keep the distance away from the object the same, and just adjust the X/Y.

Comment
Add comment · Show 7 · 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 Eowyn27 · Jan 06, 2014 at 10:20 PM 0
Share

I'm getting the error: Error CS1519: Invalid token ';' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp-firstpass)

avatar image Jinxology · Jan 06, 2014 at 10:30 PM 0
Share

Ahh, you are using C#, but my code example is in Javascript.

avatar image Eowyn27 · Jan 06, 2014 at 10:31 PM 0
Share

I rewrote in C#:

 public Camera mainCam;
  
 void On$$anonymous$$ouseUp() {

  mainCam.transform.position = new Vector3(transform.position.x, transform.position.y, mainCam.transform.position.z); 
 
    
 }

However, I realized I need to add more detail. $$anonymous$$y main camera is looking at several objects right now. When I click one object, it zooms into the object and deactivates the other objects. However, there's another object on that zoomed in object, so that when I click on that object, I want the camera from that view to move to the new object (x,y) distance away. Right now, it's only moved a little bit from where the zoomed in object is and not toward the other object.

So, basically, I want to set wherever the camera is currently to the other object far, far away

avatar image Jinxology · Jan 06, 2014 at 10:41 PM 0
Share

Hmm, not sure, sorry, you lost me there.

avatar image Eowyn27 · Jan 07, 2014 at 12:41 AM 0
Share

Actually, the problem is that the camera is too zoomed in when I put the coordinates of where the camera should be like manually entered in the vector3 above. hmm

Show more comments

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.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

getting the position of player for the minimap to follow 1 Answer

How do you position an object relative to the camera? 0 Answers

How can I modify this rotation code to change how far the object rotates? 0 Answers

How to get the constant Y rotation of the camera 2 Answers

C# 2d Spawn Randomly along Camera Borders 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