- Home /
How to synchronize both movement and rotation?
Hello everyone,
I'm trying to make a camera that, when I click on a button, will move towards a null object that is already positioned and rotated where I want it to be.
I have this code:
void Update()
{
if (moveCam == true)
{
cam.transform.position = Vector3.MoveTowards(cam.transform.position,
target.transform.position, (speed + Mathf.Abs(Vector3.Distance(cam.transform.position,
target.transform.position))) * Time.deltaTime);
}
}
The code works great, as the camera moves faster when far away from the target, and slower as it approaches it. However, the rotation has left me scratching my head. I've tried using the same method (getting the distance between the two points) with Quaternion.RotateTowards() to synchronize both, but sadly I can't get it to work.
What would be the best way to make sure that they both end at the same time?
Answer by Davinder_Singh · Oct 18, 2021 at 09:26 AM
Hi! You can use the distance between target position and camera to speed up and slow down rotation of camera. Further the camera from target, faster the rotation and vice versa! For making them end at same time, you can use percentage of distance remaining and rotation remaining rotate accordingly.