- Home /
How can i rotate the camera to the player facing direction ?
The script is attached to the Main Camera. The player is ThirdPersonController.
Now when i move the player the camera follow him but not rotating so if i move the player to the right the camera will follow it and move to the right. Instead i want the camera to rotate to where the place is facing and follow him. So if the player is facing to the right and move to the right rotate the camera to this direction and be behind the player. Now the camera is not behind the player it's beside the player.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
transform.LookAt(player.transform);
}
}
Answer by edo_m18 · May 11, 2017 at 01:40 AM
My understanding is you want camera to follow behind him, right? Your code is only applied an offset from him. so you should consider a rotation of him. Maybe my code is work well, please try it.
transform.position = player.transform.position + (-player.transform.forward * offset.magnitude);
Note: above code is a little bad. so you should cache an offset magnitude because the magnitude is a little heavy for calculation.
Your answer
Follow this Question
Related Questions
How can i use a bool to decide when to move the character or not ? 2 Answers
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers