• 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 boazlaurens · Oct 18, 2014 at 04:37 PM · camerarotationlookatfollowlookrotation

Camera following/looking at aircraft

I have the following code so the camera follows the aircraft.

 Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, transform.position - transform.forward * 10f + transform.up * 2f, 5f * Time.deltaTime);
 Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, transform.rotation, 5f * Time.deltaTime);

I want the camera to be slightly higher the aircraft so you don't just see the back. That's why there is an offset in the position. This is working fine, except now the camera is looking 'over' the aircraft, in stead of 'at'. How do I make sure the camera looks at the aircraft, but still follows it's rotation? 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

1 Reply

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

Answer by nyonge · Oct 18, 2014 at 05:27 PM

1) Create an empty game object that is a CHILD of your aircraft. Name it CameraTarget.

2) Set it where you want the camera to be (5 units behind, 3 units above the jet or something).

3) Make the camera's position follow THAT gameobject, because since it's locally tied to the jet it'll always be in the right spot.

4) Make the camera still rotate towards your aircraft, the rotation code posted should be fine since it's in worldspace.

5) Profit!

At least, that SHOULD work. Depending on any child/parent hierarchies you might have on your camera and aircraft, you might have to play around with local space vs. world space.

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 boazlaurens · Oct 18, 2014 at 05:38 PM 0
Share

Still doesn't work.. When I add:

Camera.main.transform.LookAt (transform.position);

The camera doesn't rotate with the aircraft anymore when it rolls..

avatar image nyonge · Oct 18, 2014 at 05:42 PM 0
Share

Oh, I see, when it rolls too. Don't use LookAt, LookAt will make the camera face towards the aircraft from the position that it's in. What you want to do is move the camera to the position as described above, but match the aircraft's angle of rotation, like the code in your original post.

avatar image boazlaurens · Oct 18, 2014 at 05:45 PM 0
Share

Yeah, but when I do that, the camera looks 'over' the aircraft in s$$anonymous$$d of 'at', because the camera is higher than the aircraft..

avatar image nyonge · Oct 18, 2014 at 05:53 PM 0
Share

Ahhhh okay, I gotcha. Okay, create a new public Vector3 called "RotationOffset" and set it to the additional degrees that you'd want your camera to face; for example, if you want it to face ten degrees down, do new Vector3(10f,0f,0f); or something like that. It's public, so you can play around with it.

Now, add that rotation vector to your current rotation target. So ins$$anonymous$$d of just

 Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, transform.rotation, 5f * Time.deltaTime);

Try doing

 Quaternion RotationTarget = transform.rotation * Quaternion.Euler(RotationOffset);
 Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, RotationTarget, 5f * Time.deltaTime);

So you create the RotationTarget by adding the transform.rotation and Quaternion from the Euler of RotationOffset. (Euler is how you convert a Vector3 to a Quaternion. Also, you add two Quaternions by multiplying them together. $$anonymous$$ath! $$anonymous$$ore info: http://answers.unity3d.com/questions/130510/add-or-substract-two-quaternionsrotations.html )

Hopefully that works!

avatar image boazlaurens · Oct 18, 2014 at 06:01 PM 0
Share

Great solution! Thank you so much! Have a nice day.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera not rotate when following player 1 Answer

"Look rotation viewing vector is zero" error 2 Answers

Rotation using Unity2D 3 Answers

rotate gameobject 1 Answer

Why LookRotation works, but LookAt doesnt 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