- Home /
How to make camera follow player but not when they move?
Would really appreciate any help on this as I have been trying all day.
The image is moving automatically and the camera follow it as a child, but the camera also follows whenever the image moves left or right. When the player moves, I would like for the camera to stay still, but still following them where they are automatically moving.
float distance = 10f;
float speed = 0.1f;
Vector3 direction = new Vector3(1f, 0f, 0f).normalized;
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.Translate(direction * -distance);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.Translate(direction * distance);
}
transform.Translate(0F, 13F, 0 * Time.deltaTime);
}
Thanks for any help
Answer by DESTRUKTORR · Oct 02, 2015 at 11:10 AM
I'm assuming you mean that you only want the camera to move when you force the player to move? Like when transitioning areas/screens? There's a lot of details about this you'll need to clarify to be sure anyone can answer this, but you may want to just make the camera's transform not set as a child of the player's transform, and just move it in an update function.
void Update()
{
cam.transform.position = Vector3.Lerp(cam.transform.position, cam_desired_position, cam_speed * Time.deltaTime);
}
where cam
is the camera, cam_desired_position
is the position you want the camera to move to, and cam_speed is the speed you want to move the camera in units/second. Just set this from another function that references that script.
Thanks, but if the player is sometimes speeding up / slowing down, additional will need to be added?