- Home /
Camera follow player's rotation
Hello! I'm learning Unity and I'd like the camera to follow my player's rotation so the camera it's always at its back. Here's the code I'm using for the camera to follow my player's movements. What do I have to change?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamaraController : MonoBehaviour {
public GameObject jugador;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - jugador.transform.position;
}
void LateUpdate () {
transform.position = jugador.transform.position + offset;
}
}
Answer by wewewu · Jun 19, 2020 at 02:16 PM
Simply make the camera to the player's child or create a transform in player and lerp camera to the transform and look at the player.
The camera has already a transform code and it follows the player's movements but not its rotation
Or you can multiply player.transform.forward to the offset to make it local.
Answer by Saiansh2525 · Jun 19, 2020 at 08:47 PM
Basically drag and drop the camera onto the player in the editor.
Answer by Artik2442 · Jun 20, 2020 at 01:59 PM
The better thing to do is the transform.LookAt(player);
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamaraController : MonoBehaviour {
public GameObject jugador;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - jugador.transform.position;
}
void Update () {
transform.LookAt(jugador);
}
void LateUpdate () {
transform.position = jugador.transform.position + offset;
}
}
Your answer
Follow this Question
Related Questions
Fixing My Avatar To Have A Humanoid Rig,How To Fix My Avatar To Be Humanoid Rigged? 0 Answers
3d animation controller 0 Answers
3d Sound not working? 0 Answers
TPS Camera roll ball 0 Answers