- Home /
Need help updating camera position to go above the player
I'm quite new to this. What I'm trying to do is use a script to set the Camera's position to be the same as the Player's position. I used a script to do this instead of parenting so that the camera retains it's rotation information. The problem here is that the camera is now inside of my Player, but I just want the camera to be slightly above and behind the player, in order for the Player to be seen. Here's my camera script--
public class CameraMove : MonoBehaviour
{
public Transform PlayerTransform;
public Transform CameraTransform;
public bool PlayerAlive = true;
void Update()
{
if (PlayerAlive == true)
{
CameraTransform.position = PlayerTransform.position;
}
}
}
I took the Player's Transform and put it into the script in the Inspector window, same thing with the Camera Transform.
I'm assuming there's some way to add/subtract to the xyz-axis(es?), but I've got no idea how to do that. Any help?
Answer by SteenPetersen · Feb 24, 2019 at 06:14 PM
The problem is that that code will make the camera have the exact same position as the player. you dont want that. you want an offset:
public class CameraMove : MonoBehaviour
{
public Transform PlayerTransform;
public Transform CameraTransform;
public bool PlayerAlive = true;
[SerializeField] Vector3 offSet;
void Update()
{
if (PlayerAlive == true)
{
CameraTransform.position = PlayerTransform.position + offSet;
}
}
}
Then, in the inspector, set your offset to be about {0, 10, 0} and test that. That should set the camera about 10 units on the y Axis (making the assumtion that (Y) is "up" for you) above the player.
Answer by Cornelis-de-Jager · Feb 24, 2019 at 09:08 PM
What you want to do is create an empty gameobject, parent it to the Player and move it to the position you want the camera to be. You then simply create a script where the camera follows the position of that empty gameobject.
public class CameraFollow: MonoBehaviour {
pubic Transform objToFollow; // <--- Reference that object here
public float followDelay; // Smooths out the following effect
void LateUpdate => transform.position = Vector3.Lerp (transform.position, objToFollow.position, followDelay);
}