• 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 · Oct 11, 2013 at 06:15 PM · rotationlookatfollowsmooth

Add rotation to LookAt (CarSmoothFollow) ?

Hi,

I would like to add a rotation to the camera, which already uses a LookAt method. When I add this code to a file :

 void Update () {
         float newZ = Input.GetAxis("Horizontal") * -1 * 25;
         Vector3 rotEnd = new Vector3( 0, 0, newZ );
         transform.eulerAngles += rotEnd;
     }


the camera does not rotate with the horizontal axis because of the lookAt method in the CarSmoothFollow script. I am trying to add it directly in the lookAt method, but it does not work correctly, it kind of repeats the rotation 100 times per second. Would you know how to do this? Here is the code :

     void LateUpdate () {
         wantedHeight = target.position.y + height;
         currentHeight = transform.position.y;
         
         //HORIZONTAL
         float newZ = Input.GetAxis("Horizontal") * -1 * 25;
         Vector3 rotEnd = new Vector3( 0, 0, newZ );
         //transform.eulerAngles += rotEnd;
  
         wantedRotationAngle = target.eulerAngles.y;
         currentRotationAngle = transform.eulerAngles.y;
  
         currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
  
         currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
  
         wantedPosition = target.position;
         wantedPosition.y = currentHeight;
  
         usedDistance = Mathf.SmoothDampAngle(usedDistance, distance + (parentRigidbody.velocity.magnitude * distanceMultiplier), ref zVelocity, distanceSnapTime); 
  
         wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
  
         transform.position = wantedPosition;
         transform.LookAt(target.position + lookAtVector + rotEnd);//wrong
     }


Thanks

Comment
Add comment · Show 2
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 Tomer-Barkan · Oct 11, 2013 at 06:47 PM 0
Share

If you rotate it in the Update() method, it will keep rotating every frame... Why don't you explain what you're trying to achieve exactly and we'll try to help.

avatar image Pauls · Oct 11, 2013 at 07:42 PM 0
Share

@tbkn thanks for the comment, robertbu gave the right solution, the goal was to lean the camera to one side or the other, when the object turns to one side. It works fine now, thanks anyway!

1 Reply

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

Answer by robertbu · Oct 11, 2013 at 06:50 PM

It is hard to make a best fix without knowing the geometry of your situation. Let me answer the specific question you ask. I think you want:

 transform.LookAt(target.position + lookAtVector);
 transform.eulerAngles += rotEnd;

Note that given you have a look at vector (instead of a position), you can do this:

   transform.roation = Quaternion.LookRotation(lookAtVector);
   transform.eulerAngles += rotEnd;

And you should be able to combine the two concepts like this (untested):

 transform.rotation = Quaternion.Euler(rotEnd) * Quaternion.LookAt(lookAtVector);
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 Pauls · Oct 11, 2013 at 07:40 PM 0
Share

Thank you! So the first one works fine, I don't know why it does not work when using another file, with the Update method (while CarSmoothFollow uses LateUpdate) and located above in the inspector (don't know if the order changes anything). About the second and third solutions, the camera is trembling but the effect still works, and there is a note in the console about lookRotation with "zero". The goal was to lean the camera to one side or the other, when the object turns to one side. Anyway, thank you very much, it works fine now!

avatar image robertbu · Oct 11, 2013 at 08:08 PM 0
Share

LookAt() handles a Vector3.zero situation. For the other two, you would need to place them inside an if() if you code is generating Vector3.zero for lookAtVector:

 if (lookAtVector != Vector3.zero) {
avatar image Pauls · Oct 12, 2013 at 10:31 AM 0
Share

Ok thank you!

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

16 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

Related Questions

Bones [or something] to 'follow' or 'chase' each other! 1 Answer

smooth transform look at with space self rotation 0 Answers

smooth look at with offset 0 Answers

Making a camera that follows a rigidbodied sphere. 2 Answers

rotate gameobject 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