Camera rotation relative to GameObject's rotation

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!

Why dont you just put the camera insde the car object hierarchy? You can also do it using code:

  myCamera.transform.parent = myCar.transform;

This is simply the easiest way but if you want you can aslo get EulerAngles of the car and put them in some temporary variables for camera in each update call, then add the generated x and y rotations from mouse look to those temp vars and put the sum in your final transform.rotation

This should also do the work

So I figured it out and thought I’d share for anyone else trying to achieve similar:

    //get the Y-rotation adjustment to follow the car
	playerYRotationCurrent = player.transform.eulerAngles.y;
	playerYRotationUpdate = playerYRotationCurrent - playerYRotationPrev;
	playerYRotationPrev = playerYRotationCurrent;
	
	yRotation += Input.GetAxis("Mouse X") * lookSensitivty + playerYRotationUpdate;

If there is a more elegant way of doing this, feel free to update it :slight_smile: