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 firestorm713q · Feb 26, 2013 at 10:49 PM · cameraphysicsrigidbody

Why is my Camera's Position updating slower than my Rigidbody's Position?

Simply put, I'm trying to do a Top-Down space shooter, using rigidbody physics so as to simulate collisions, etc. For whatever reason, as the rigidbody gains speed, it slowly starts moving off screen. Collision also seems to break. Here's the code:

     using UnityEngine;
     using System.Collections;
     
     public class InputHandler : MonoBehaviour 
     {
         Transform Pcamera;
         Quaternion PlayerRotation;
         Transform PlayerMesh;
         Transform myTransform;
         
         float velX;
         float velZ;
         float maxVel = 25;
     
         public float speed = 2.0f;
         public Rigidbody rbody;
         
         
         void Awake() {
             myTransform = transform;
         }
     
         void Start () {
             Pcamera = GameObject.Find("Main Camera").transform;
             PlayerMesh = GameObject.Find("PlayerMesh").transform;
             PlayerRotation = PlayerMesh.rotation;
             Velocity = PlayerMesh.rigidbody.velocity;
             velX = PlayerMesh.rigidbody.velocity.x;
             velZ = PlayerMesh.rigidbody.velocity.z;
         }
         
         void FixedUpdate () {
             HandleInput();
             PMeshRotate();
             velX = PlayerMesh.rigidbody.velocity.x;
             velZ = PlayerMesh.rigidbody.velocity.z;
             Velocity = PlayerMesh.rigidbody.velocity;
             VelocityChecker();
         }
         
         void Update() {
             myTransform.position = PlayerMesh.position;
             
             Vector3 CameraPosition = myTransform.position;
             CameraPosition.y = Pcamera.position.y;
             Pcamera.position = CameraPosition;
         }
         
         void VelocityChecker() {
             if(PlayerMesh.forward.x * PlayerMesh.rigidbody.velocity.x>maxVel)
                 velX = maxVel;
             if(PlayerMesh.forward.x * PlayerMesh.rigidbody.velocity.x<-maxVel)
                 velX=-maxVel;
             if(PlayerMesh.forward.z * PlayerMesh.rigidbody.velocity.z>maxVel)
                 velZ = maxVel;
             if(PlayerMesh.forward.z * PlayerMesh.rigidbody.velocity.z<-maxVel)
                 velZ=-maxVel;
             PlayerMesh.rigidbody.velocity = new Vector3(velX, 0, velZ);
         }
         
         void HandleInput() {
             if(Input.GetKey(KeyCode.W)) {
                 PlayerMesh.rigidbody.AddRelativeForce(0, 0, 5);
             }
             if(Input.GetKey(KeyCode.S)) {
                 PlayerMesh.rigidbody.AddRelativeForce(0, 0, -5);
             }
             if(Input.GetKey(KeyCode.D)) {
                 PlayerMesh.rigidbody.AddRelativeForce(5, 0, 0);
             }
             if(Input.GetKey(KeyCode.A)) {
                 PlayerMesh.rigidbody.AddRelativeForce(-5, 0, 0);
             }
         }
         
         void PMeshRotate() {
             Vector3 mousePos = Input.mousePosition;
             mousePos.z = -10;
             Quaternion temprotation = Quaternion.LookRotation(PlayerMesh.position - Pcamera.camera.ScreenToWorldPoint(mousePos), Vector3.up);
             PlayerRotation = Quaternion.Slerp(PlayerRotation, temprotation, Time.deltaTime * 5.0f);
             PlayerRotation.z = 0;
             PlayerRotation.x = 0;
             PlayerMesh.rigidbody.MoveRotation(PlayerRotation);
         }
     }


Here's also a link to my project, so you can mess with movement to see what I mean. Controls with the mouse, WASD moves you relative to the player, space to reset if you lose track of the player.

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 Wiki

Answer by firestorm713q · Feb 27, 2013 at 03:09 AM

Alright, I figured it out. Thanks for the help. LateUpdate was part of the solution. Basically, what needed to happen was the camera needed to follow the mesh, but not rotate with it. So I attached the Camera to the PlayerMesh, and the above script to the Playermesh as well. Finally, I had to figure out how to lock the camera's rotation.

I misunderstood how Update, FixedUpdate, and LateUpdate worked, so I played around. What I needed was for the camera to move after the physics calculations, apparently, so I needed to use LateUpdate. Basically, I took all of the camera control out of the above script, and used the following script to control the camera:

      void Awake()
     {
         InitialRot = transform.rotation;
     }
     void LateUpdate ()
     {
         transform.rotation = InitialRot;
         transform.position = myposition;
     }

Remember, this must be done in FixedUpdate() otherwise your camera won't update quickly enough.

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

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

Orbit cam and Rigidbodies aiming 0 Answers

Camera Stuck on Wall Corners 0 Answers

Camera follow - rotating rigidbody 1 Answer

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

Strange problem with cloth 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