MoveTowards on mouse click

I’m trying to make it so that when you press the left mouse button, the player’s camera moves to the object. The problem is that when you press the mouse, the camera does not have time to get closer to the object.

void Update()
{
    if (Physics.Raycast(_camera.transform.position, _camera.transform.forward, out var hit, _distanceMax) && hit.transform.tag == "Dynamic")
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Debug.Log("Ok");
            _camera.transform.position = Vector3.MoveTowards(_camera.transform.position, hit.transform.position, speed * Time.deltaTime);
        }
    }
}

You’re using GetButtonDown, which is only true for the first frame you click the mouse button. If you use GetButton instead, it will be true for every frame that the mouse button is held down.

1 Like