- Home /
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.
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.
Your answer
Follow this Question
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