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 Chryb · Feb 14, 2013 at 08:29 PM · camerapositionrotate

How to rotate the camera to the gameobject position.

I have a simple sphere Gameobject at position (0, 0, 0). My camera has the beginning rotation (0, 0, 0). Now i have set the inputs MouseX and MouseY to get the x, y and change the position of the camera.

 if (mouseDown){
       x = Input.GetAxis("MouseX");
       y = Input.GetAxis("MouseY");
 }

That works all fine, but now i want to set the camera rotation to the center of the Sphere (0, 0, 0). So that I can rotate around (with holding mouse0) the sphere. I tried it with

   camera.transform.rotation.SetFromToRotation(camera.transform.position, target.transform.position);

but it don't work.

I'm sure that it easy but i don't find the solution. I'm absolutely new in Unity. Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Loius · Feb 14, 2013 at 08:34 PM

The simplest solution is to create a pivot object for the camera (as the parent of the camera), and move/rotate that object instead of the camera itself.

Comment
Add comment · Show 3 · 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 Chryb · Feb 14, 2013 at 08:47 PM 0
Share

It is necessary that the camera rotates around the gameObject. I want to create a astroid as the center and rotate the camera (with mouse0 key down) around the object in the same distance.

avatar image Chryb · Feb 14, 2013 at 09:24 PM 0
Share

I already tried:

camera.transform.rotation.SetFromToRotation(camera.transform.position, target.transform.position);

but that don't work.

avatar image Chryb · Feb 15, 2013 at 09:51 AM 0
Share

Noone help?

avatar image
0

Answer by Chryb · Feb 15, 2013 at 01:53 AM

Now, the Camera has the right rotation. I realized that with camera.transform.LookAt(astroid.position);, but now i have a other problem. The position of the camera is not perfect set and the camera don't stop to rotate around the astroid if i start to drag the mouse.

That I use for the position:

 if (mouseDown){
     x += Input.GetAxis("MouseX");
     y += Input.GetAxis("MouseY");
 }
         
         SetCameraRotation();
         SetCameraPosition(rotation, x, y);
 }
 
 void SetCameraRotation(){
     camera.transform.LookAt(astroid.position);
 }
     
 void SetCameraPosition(Quaternion rotation, float x, float y){
     Quaternion unit = Quaternion.Slerp(camera.transform.rotation, astroid.position, Time.deltaTime * 3);
     Vector3 position = unit * new Vector3(x * 0.1f, y * 0.1f, 0.0f) + camera.transform.position;
     camera.transform.position = position;
 }

I hope someone can help me.

Comment
Add comment · Show 3 · 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 Chryb · Feb 15, 2013 at 02:58 PM 0
Share

Noone help?

avatar image Chryb · Feb 15, 2013 at 10:07 PM 0
Share

Really too hard?

avatar image Chryb · Feb 16, 2013 at 02:04 AM 0
Share

Thats the new code:

 if (mouseDown){
 
             x += Input.GetAxis("$$anonymous$$ouseX");
 
             y += Input.GetAxis("$$anonymous$$ouseY");
 
         }
 
         
 
         Quaternion rotation = Quaternion.Slerp(camera.transform.rotation, Quaternion.Euler(x, y, 0), Time.deltaTime * 3);
 
         
 
         SetCameraRotation();
 
         SetCameraPosition(rotation, x, y);
 
         
 
         x = 0;
 
         y = 0;
 
         
 
     }
 
     
 
     void SetCameraRotation(){
 
         camera.transform.LookAt(astroid.position);
 
     }
 
     
 
     void SetCameraPosition(Quaternion rotation, float x, float y){
 
         Vector3 position = rotation * new Vector3(x * 3, y * 3, 0.0f) + camera.transform.position;
 
         camera.transform.position = position;
 
     }
avatar image
0

Answer by robertbu · Feb 16, 2013 at 01:09 AM

My understanding is that you have an object at the origin (asteroid), and you want a camera "above" the asteroid looking "down" (towards the origin). And you want to move the mouse to rotate the camera round the asteroid as if you are skimming along the surface looking down.

To do this, I would 1) place an empty game object at the center of rotation, 2) place the camera the right distance looking at the asteroid, 3) make the camera a child of the empty game object, and 4) attach a script to the empty game object that rotates the empty game object. The camera will be taken along for the ride and remain a fixed distance above empty game object. Here is simple few lines of code based on your GetAxis() above to rotate the empty game object:

 void Update () {
         if (Input.GetMouseButton(0)){
             Vector3 v3T = new Vector3(Input.GetAxis("Vertical"),-Input.GetAxis("Horizontal"),0.0f);
             transform.Rotate (v3T *0.1f);
         }
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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Camera rotation around player while following. 6 Answers

Aircraft wing position 0 Answers

Scripting Issue With Top Down Mouse Movement 1 Answer

Changeing Camera Position 1 Answer

Rotating Camera around Player at Certain Point in Map 0 Answers


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