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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Pauls · Mar 19, 2013 at 01:38 PM · cameraquaternionrotatelookat

rotate camera to face a specific object?

Hi,

I would like to move my camera at a specific moment, and make it look at a plane with an animated texture. So I would like to copy the rotation of the plane (that is outside of the landscape), and move the camera a little bit backwards so that it can look correctly at the plane.

But I can't make it work: the first chunk of code does not rotate properly and the angle is not good, and the second chunk of code does not look at all at the plane ... Would you know how to do this?

Here is the code :

 void Update () {
         
         if (moveCamera){
             /*transform.position = toFollow.position;
             transform.eulerAngles = toFollow.eulerAngles;
             transform.position += Vector3.up * 8;
             transform.LookAt(toFollow.position);
             */
             
             animation.Stop();
             Vector3 relativePos = toFollow.position - transform.position;
             Quaternion rotation = Quaternion.LookRotation(relativePos);
             transform.rotation = rotation;
             transform.position = toFollow.up * 8;
         }
     }



EDIT :

Here is the image with :

transform.forward = -toFollow.up; (and the angle on the last image on the right) :

125alt text125

If I play with Quaternion, I only get a result with

transform.rotation *= Quaternion.FromToRotation(transform.up, -toFollow.forward); , but in this case it gives me exactly the same as the image above. ?

Thanks

transform.png (13.3 kB)
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 whydoidoit · Mar 19, 2013 at 01:44 PM

Yeah planes are a pain because they kind face in the wrong direction!

This might well work for you:

 transform.position = toFollow.position - toFollow.up * 8;
 transform.forward = -toFollow.up;

or

  transform.rotation = Quaternion.LookRotation(-toFollow.up, Vector3.up); //Not sure what your up direction should be here
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 Pauls · Mar 19, 2013 at 05:10 PM 0
Share

Thank you very much! It works fine, with "+ toFollow.up"! The second option works also, but the camera is not exactly at the right rotation with both ways, it is a bit rotated to the right ? I have tried to copy the euler coordinates but Unity does not allow me to do it, would you a way to achieve this? The camera faces in the right direction, it is parallel to the plane, but with a slight angle to the right...

avatar image whydoidoit · Mar 19, 2013 at 05:15 PM 0
Share

Right that makes sense - neither of those is caring about the rotation about the axis facing the plane - how about this:

  transform.forward = toFollow.up;
  transform.rotation *= Quaternion.FromToRotation(transform.up, toFollow.forward); 

That might be toFollow.right (ins$$anonymous$$d of forward) or -that or -.right!!!

avatar image Pauls · Mar 20, 2013 at 05:41 AM 0
Share

@whydoidoit : thanks a lot, so I edited my first post with an image, I am not sure which values to rotate, -toFollow.forward faces the plane but with the same angle as before! I might be doing something wrong, so I posted the image above! Would you have any idea? Thanks again!

avatar image whydoidoit · Mar 20, 2013 at 09:15 AM 1
Share

Ah to hell with it :) It's one of those combinations of right or forward!

Let's try this:

     transform.forward = toFollow.up;
     var angles = transform.localEulerAngles;
     angles.z = 0;
     transform.localEulerAngles = angles;
avatar image Pauls · Mar 20, 2013 at 11:55 AM 0
Share

Alright! It finally works! I changed z in ".y = 0" and I changed the plane's rotation, with "0, 180, 0"! Thanks a lot for your help!

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

11 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

Related Questions

How do you check if Quaternion.Slerp has finished 5 Answers

Using MouseLook script with LookAt function 2 Answers

How to rotate an object depending on the rotation of an other Object 3 Answers

Use lookAt and transform.translate at the same time 1 Answer

Rotate camera around player 2 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