- Home /
Camera not rotate when following player
Hello I am making a 2D game but I need the camera to follow the player (which Is a sprite) without rotating. For example my code for the player includes a rotation for the player to face the other side when he walks that way. But whenever I attach the camera to the player the camera follows the player when he rotates, therefore you are able to see the backside of the level, I was wondering if it is possible to have the camera follow the player without it rotating to the back of the level, and have the player still able to face the direction he is walking in. Here is the script attached to the player
using UnityEngine;
using System.Collections;
public class playercontrol : MonoBehaviour {
void Update ()
{
Movment ();
}
void Movment()
{
if(Input.GetKey (KeyCode.D))
{
transform.Translate(Vector2.right *4f* Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if(Input.GetKey (KeyCode.A))
{
transform.Translate(Vector2.right *4f* Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180);
}
if(Input.GetKey (KeyCode.W))
{
transform.Translate(Vector2.up *6f* Time.deltaTime);
}
}
}
Sorry if this sounds over complicated, Thanks
Answer by MakinStuffLookGood · Jan 23, 2015 at 06:48 PM
Rather than parenting your camera to the player, give the camera a script to follow the player.
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform Player;
public Vector3 Offset;
void LateUpdate ()
{
if (Player != null)
transform.position = Player.position + Offset;
}
}
Just drag your player into the Transform field on the inspector, and set the desired offset (you should include some Z distance back away from the player, and usually some Y height so the player isn't in the center of the screen, but instead sits at the bottom like in any 2D Mario).
Hey question how can I set the rotatation using this same script this is beautiful but I want the camera to look down a little.