- Home /
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'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.
Answer by Sooper1337 · Apr 13, 2013 at 04:26 PM
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;
}
}
Your answer
Follow this Question
Related Questions
Camera not following Player 1 Answer
How to avoid TouchPhase.Ended effect? 0 Answers
Make object follow mouse, while actually moving camera, instead of object 2 Answers
Quaternion.Slerp in a semisphere 1 Answer
Menu below camera - for VR 0 Answers