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 kr00r4n · Apr 10, 2014 at 07:11 PM · cameraupdatefollowfixedupdate

Follow camera: Player jumps on move

I am trying to create a follow camera. I have isolated the problem to the following simple setup:

1) Player object with rigidbody, no gravity, drag 4 and the following simple script

 public class playercontrol : MonoBehaviour {
     void FixedUpdate () {
         if (Input.GetKey (KeyCode.Space)) {
             rigidbody.velocity = transform.forward * 20;
         }
     }
 }

2) Camera with the following script

 public class cameracontrol : MonoBehaviour {
     public GameObject player;
     void FixedUpdate () {
         transform.position = player.transform.TransformPoint (new Vector3 (0f, 1.5f, -4f));
         transform.rotation = player.transform.rotation;
     }
 }

The intent is to have the camera always placed some distance behind the player. However, when i press the space key the player instantly jumps some distance forward, the camera follows it smoothly thereafter. When i release the space key the player slows down due to drag and, on the screen, it slowly moves backwards the distance that it jumped earlier.

Now, if i change the camera function to Update instead FixedUpdate this problem doesn't occur. I understand the difference between Update and FixedUpdate, but i dont understand why the player would jump forward even when the camera is simply copying the position directly.

Comment
Add comment · Show 1
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 robertbu · Apr 10, 2014 at 07:22 PM 0
Share

Reading keyboard input inside of FixedUpdate() can result in lost keyboard events, though Get$$anonymous$$ey() likely will be okay. Typically you want camera follow code to be executed in LateUpdate(), and there is no reason why your current code should be executed in FixedUpdate(). I don't know why you see a jump, but it seems to be that you'd want your movement code to ease up to the maximum velocity:

 rigidbody.AddForce(someForce * transform.forward);
 rigidbody.velocity = Vector3.Clamp$$anonymous$$agnitude(rigidbody.velocity, 20f);

1 Reply

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

Answer by Mikael-Gyth · Apr 10, 2014 at 07:34 PM

When you apply a force in FixedUpdate the physics calculations are calculated immediately after. If you also move the camera in FixedUpdate the movement of the object have not been calculated yet and as a result the object would have moved when the frame gets rendered. The preferred place to put CameraMovement is in LateUpdate where you can be sure that all movement calculations have been completed. This is better explained here.

Comment
Add comment · Show 2 · 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 kr00r4n · Apr 10, 2014 at 07:47 PM 0
Share

This makes sense. So all the engine physics calculations (in this case calculating the translation based on the velocity) happen after FixedUpdate has been called on all the objects? so my camera is moving to the older position?

avatar image Mikael-Gyth · Apr 11, 2014 at 05:11 PM 0
Share

Yes, that is correct.

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

22 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 avatar image avatar image avatar image

Related Questions

Camera Jitter using Fixed or LateUpdate, need it to be smooth in both. 2 Answers

How to restrict rotation properly 2 Answers

Should player input be caught in Update, and then raycasting in FixedUpdate for shooting mechanics? 1 Answer

Camera 2d following player with delay jumps 1 Answer

Camera not rotate when following player 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