- Home /
unwanted movements when camera follows object
Hi,
i would like to add a value to the camera's position so that it can look at an object from its right side when it is moving. But when i add this small value : Vector3.right * distanceRight, the camera sometimes seems to hit something, and moves from left to right just during a short time ( like 0.1 sec ) several times on the path.
The camera looks at "targ", an empty object, which is a child of a moving sphere, and nothing is on the way of the sphere when the sphere moves, so i guess the problem comes from this line : transform.position += Vector3.right * distanceRight;
Would you know how i could get a smooth tracking with this code? (please see at the bottom, the line with / HERE / )
function LateUpdate () {
// Early out if we don't have the target
if (!targ)
return;
if ( m_player.straight ){
distance = 4.0;
distanceRight = 0.0;
height = 3.0;
} else if ( m_player.right ) {
distance = 1.8;
distanceRight = 0.2;
height = 0.6;
}
// Calculate the current rotation angles
var wantedRotationAngle = targ.eulerAngles.y;
var wantedHeight = targ.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the targ
transform.position = targ.position;
//transform.position -= Vector3.forward * distance;
transform.position -= currentRotation * Vector3.forward * distance;
transform.position += Vector3.right * distanceRight; /* HERE */
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the targ
transform.LookAt (targ);
}
Thanks
From a quick look, maybe this line has issues when the rotation passes across the 0.0/360.0 boundry?
transform.position -= currentRotation Vector3.forward distance;
@robertbu : Thanks! i played with it a bit, i don't understand completely what the rotation does in this case, but you were right, if i remove the rotation only it works perfectly! Thanks
Your answer
Follow this Question
Related Questions
How can I accurately convert a Game Object's position to a sub Canvas/Rect screen position? 0 Answers
Having a plane at exactly the far clip of a camera 1 Answer
How to implement lock on with a camera that is not a child of the player object? 1 Answer
Move camera relative to current position 0 Answers
FPS animate camera look up/down 0 Answers