Hi!
It’s my first time making a CameraFollow script. It’s just a simple one. I used this code:
public class CameraFollow : MonoBehaviour
{
public GameObject player;
private void Update()
{
Vector2 playerPosition = new Vector2(player.transform.position.x, player.transform.position.y);
//follow player
transform.position = playerPosition;
}
}
But when I enable the script and play the game, I no longer see the game, just a solid color. And if I disable the script, it goes back to normal and I see the game again. I don’t get it. I didn’t change anything else and it did the same thing on another tutorial project I was working on. Does anyone know why this happens?
It’s because you are setting your camera to the exact position of your character. Your camera is probably inside your player, this is more how to do 1st person camera. If you want 3rd person or 2d sidescroll you need to position your camera away from player but pointing at them. There is look at for just this purpose. Alos moving your camera in update will look terrible! You need to move your camera in LateUpdate().
Maybe you aren’t keeping a distance between the player and the camera
Thank you both so much!! I really appreciate it. That was the exact problem. I had to move the camera back on the z axis so that it wasn’t on top of the player. For any newbies like me, if you ever get this problem, this is the corrected code I used:
{
public GameObject player;
private void LateUpdate()
{
Vector3 playerPosition = new Vector3(player.transform.position.x, player.transform.position.y, -10);
//follow player
transform.position = playerPosition;
}
}