- Home /
Camera follow the player code
I am trying to get the main camera follow the player movement. Here is my code to do so: *The playerController :
// Start is called before the first frame update
void Start()
{
directions = new Dictionary<KeyCode, Vector3> {
{KeyCode.W, Vector3.forward},
{KeyCode.S, Vector3.back},
{KeyCode.A, Vector3.left},
{KeyCode.D, Vector3.right}
};
}
// Update is called once per frame
void Update()
{
foreach (KeyCode direction in directions.Keys)
{
if (Input.GetKey(direction)) {
this.transform.Translate(directions[direction] * speed * Time.deltaTime, Space.Self);
Debug.Log("Character is moving");
}
}
}
}
*The cameraController:
public class CameraController : MonoBehaviour
{
public Transform target;
public Vector3 offset_move;
void Start() {
offset_move = transform.position - target.position;
Debug.Log("offset" + offset_move);
}
void Update() {
}
void LateUpdate() {
transform.position = target.position + offset_move;
}
}
With this code the camera position is changing but it seems that the camera isnt following the player. Can someone point out what i did wrong and help me fix this?
I tested your scripts and they are working for me. Are you sure you have set all components correctly? (namely the reference to the player gameObject)
Your answer
Follow this Question
Related Questions
Cinemachine Confiner 2D shift my camera view 0 Answers
Camera following the player in 3D - in object hirarchy or through scripts? 1 Answer
Camera smooth look at modified 2 Answers
Camera following player in Tutorial Demo (Roll A Ball) 0 Answers
2D Platformer Demo Camera Follow script doesn't follow player 1 Answer