Home > Net >  Using Vector3.Lerp to get midpoint
Using Vector3.Lerp to get midpoint

Time:11-07

I have a ball and want the main camera to follow it around so I attached a script to the camera:

public class Tracker : MonoBehaviour
{
    public GameObject target;

    void Update()
    {
        this.transform.position = Vector3.Lerp(this.transform.position, target.transform.position, .5f);
    }
}

Where the target is the ball gameobject. I want the camera to have the same x and y coordinates as the ball but keep its original Z.

Not sure what to do, maybe there's a different way of approaching this?

CodePudding user response:

You can create you movement Vector3 with a constructor for ex:

Vector3 newPos = new Vector3(target.transform.position.x,target.transform.position.y, transform.position.z);
this.transform.position = Vector3.Lerp(this.transform.position, newPos, .5f);
  • Related