- Home /
Camera transformation upon a pressing a key
Hello Devs,
When I press a key, I want the camera to move to the target gameobject. I am trying make a UI menu where when you press the key it goes to another section of the menu. In order to translate the camera the code I am using is
void trans()
{
transform.Translate(-Time.deltaTime*10, Time.deltaTime*10, 0, Space.World);
}
I want to halt it when it reaches that position.
Problem :
The camera translates but as the key press is for only a single frame it does not go beyond it. I would like the translation to be locked down to a particular x and y so as that the required gameobject is within screen. Devs, thanks.
Answer by Statement · May 02, 2012 at 11:36 PM
Consider using Vector3.MoveTowards.
// Assume target is a transform to move to (when pressed a key etc).
// Assume UpdatePosition is called every frame.
void UpdatePosition()
{
if (!target)
return;
transform.position = Vector3.MoveTowards(transform.position,
target.position,
Time.deltaTime * 10.0f);
if (transform.position == target.position)
{
target = null;
// reached target
}
}
Lasse, thank you very much. It works. but.... I do not want to change the z co - ordinates.
I got to target it to a empty gameobject. It works. Thank you Lasse.