Camera Rotation in a 2D racer

Hey all,

I’m in the process of creating a 2D racer game. Right now the camera is following the player, however I would like for it to rotate when the player turns so that it stands behind them.

Here’s an image just to show a bit more clearly what I’d like to accomplish: http://tinyurl.com/bnfsk4a

I tried attaching a cube to the front of the car and having the camera follow it and while the camera did follow the cube, it did not rotate to behind the vehicle. Any thoughts?

I've no idea what help you need.

I'm basically asking for some help rotating the camera 90 degrees when the player turns left or right so that it is positioned once more behind the car. Right now the camera will remain locked in position regardless of how many times the player turns. Hope that clears it up.

1 Answer

1

This code Should work, Just add it to your camera and set the variables in the inspector.

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	public Transform carPosition;
	public float damper;

	void Update ()
	{
		Quaternion camPosition = transform.rotation;
		camPosition.eulerAngles = Vector3.Lerp(camPosition.eulerAngles, new Vector3(camPosition.eulerAngles.x, carPosition.eulerAngles.y, camPosition.eulerAngles.z), Time.deltaTime * damper);
		transform.rotation = camPosition;
	}
}