- Home /
FPS animate camera look up/down
I'm new to unity and I'm trying to get a a "look at" to work in a first person context.
I've written code to snap to in the horizontal plane, by ascertaining the angle via the playerPosition - target position. Then zeroing out the plane we don't want to be rotating on.
And so my first person body rotates neatly around the y axis to point at the object. and re-written it a bunch of times in different contexts to understand what is going on.
But When I apply what I've learned to forcing the camera to look up or down at specific angles/at an object, it appears my understanding of what is going on falls apart (yes the camera is embeded AND i'm using localRotation).
My second attempt is to actually have a secondary "target" camera in the scene LOOKING in the direction where I want the player to be looking. Then take it's local Euler Angles. Zero out the Y and the z because according to my properties panel it's only rotated in x anyway. The previous code will take care of the rotating body around Y.
But by jove this thing is pointing in every direction but the one I want. I wind up looking at the floor or the ceiling, even though the angles are like 15 degrees. I've tried everything Local Eulers, Quaternions, vectors, using the targetcamera.transform.forward as the goal, but none of it works and they all seam to be consistently "wrong". Certain combinations work (no time delta specified so I got an un-animated snap-to, but once I started to animate it goes haywire.) I'll include some extra commented code to show some of my musings.
var cam_target_direction:Vector3 = targetcamera.transform.localEulerAngles;
cam_target_direction.y = 0;
cam_target_direction.z = 0;
var camDirectionQ2:Quaternion = Quaternion.LookRotation(cam_target_direction);
var cam_angleBetweenV2 = Mathf.Floor(Vector3.Angle (localCamera.transform.forward, gimble_target_direction));
if (cam_angleBetweenV2 > 1)
{
// localCamera.transform.localRotation.eulerAngles = Vector3.Slerp(localCamera.transform.localRotation.eulerAngles, cam_target_direction, Time.deltaTime);
// localCamera.transform.localRotation = Quaternion.Slerp(localCamera.transform.localRotation, camDirectionQ2, Time.deltaTime* damping*0.5);
localCamera.transform.localRotation.eulerAngles.x = Mathf.Lerp(localCamera.transform.localRotation.eulerAngles.x, cam_target_direction.x, Time.deltaTime * damping*0.5);
}
I can't help but feel I've missed some intrinsical "3d" know how that says you have to rotate the x component around the z component because that just the way it works. Or that the lerp, because the angles work in + and minus and the horizontal is zero, that the camera is trying to loop a whole 345 degrees, instead of nudging the camera down -15. This is like the 3rd attempt at this "y-look" in the last 3 weeks and it's beginning to get to me. I just need help.
Your answer
