So I got this far by myself:
I have a car that’s driving automatically through an array of waypoints. I have an attached camera that is rotated by the mouse. All of that works perfectly. The only problem is that if you leave the mouse and the car GameObject rotates, the camera remains facing in the direction it was facing.
I’m trying to figure out how to make it so that as the car turns, the camera follows. So you can still aim with the mouse, but its relative to the car’s rotation. If the car hasn’t turned yet, the rotation is X. If the car has turned 10 degrees to the right, the cameras rotation is is X + 10.
Here is my current code controlling the camera:
function Start () {
player = GameObject.Find("player");
cameraPositionAim = GameObject.Find("cameraPositionAim");
}
function Update () {
yRotation += Input.GetAxis("Mouse X") * lookSensitivty;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivty;
//if in Aim View, restrict the rotation of the camera to these angles
if(transform.position == cameraPositionAim.transform.position){
xRotation = Mathf.Clamp(xRotation, -80, 80);
yRotation = Mathf.Clamp(yRotation, player.transform.eulerAngles.y - 100, player.transform.eulerAngles.y + 100);
}
//otherwise, restrict the rotation of the camera to these angles
else {
xRotation = Mathf.Clamp(xRotation, -80, 0);
yRotation = Mathf.Clamp(yRotation, player.transform.eulerAngles.y - 100, player.transform.eulerAngles.y + 100);
}
xCurrentRotation = Mathf.SmoothDamp(xCurrentRotation, xRotation, xRotationV, lookSmoothDamp);
yCurrentRotation = Mathf.SmoothDamp(yCurrentRotation, yRotation, yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(xCurrentRotation, yCurrentRotation, 0);
}
If I do this:
Rotation += Input.GetAxis("Mouse X") * lookSensitivty + player.transform.eulerAngles.x;
…it freaks out, which makes sense as I imagine that is adding the rotation of the car every frame? Anyway, I’m just not sure how to accomplish this and would love a little pointer in the right direction.
Thanks!