- Home /
Rotate camera 90 degrees from target, and follow ... demo not working properly
Hello,
I'm creating a scene where you can walk a character along a path. I am trying to get the camera to follow the character at a 90 degree angle from his forward direction, which is constantly changing as he moves.
Here is a demo, use A and D to move) : Character Demo
This is the code that I have so far, which is attached to my camera, is:
var cameraYOffset : float = 10f;
var cameraDistance : float = 20f;
function LateUpdate () {
if (target) {
// Find the new camera position, 90deg right from target forward
var rightPos : Vector3 = Vector3.Cross(Vector3.up, -target.forward) * cameraDistance;
rightPos.y += cameraYOffset;
transform.position = rightPos;
// Now look at the target
transform.LookAt(target);
}
}
So this kind of works, but you'll see as the character walks further along the path (towards the end mostly), the camera sort of stays still while the character walks off into the distance. I can't figure out why this is happening. Wondering if anyone has any ideas...
Ryan
Does the camera every do anything besides sit off to the right side? Why not just nest the camera onto the player? Then it will always be right where you want.
Answer by amphoterik · Jun 26, 2013 at 02:30 PM
Try something like:
var cameraYOffset : float = 10f;
var cameraDistance : float = 20f;
function LateUpdate () {
if (target) {
// position self at target with same orientation
transform.position = target.position;
transform.rotation = target.rotation;
//moves to the side of target and up
transform.Translate(cameraDistance, cameraYOffset, 0);
// Now look at the target
transform.LookAt(target);
}
}
I still think it would be better to simply nest the camera on the target though.
Thanks amphoterik. This works pretty well. If I nest the camera, I find that it is constantly following the rotation of the parent also. Do you think it would be better to nest it and add code to keep it rotated to the world y-axis? Do you know how to do that?
Your answer
Follow this Question
Related Questions
RTS Camera Rotation and Movement 0 Answers
How to make camera rotation relative to target. 3 Answers
How do I properly rotate an FPS camera with a joystick? 1 Answer
Local Third Person Movement based off Camera Rotation 0 Answers
Camera Rotation 1 Answer