follow object unity code example
Example 1: how to make an object follow the player in unity
Add this to the zombie(s):
private GameObject wayPoint;
private Vector3 wayPointPos;
private float speed = 6.0f;
void Start ()
{
wayPoint = GameObject.Find("wayPoint");
}
void Update ()
{
wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
}
public GameObject wayPoint;
private float timer = 0.5f;
void Update ()
{
if(timer > 0)
{
timer -= Time.deltaTime;
}
if(timer <= 0)
{
UpdatePosition();
timer = 0.5f;
}
}
void UpdatePosition()
{
wayPoint.transform.position = transform.position;
}
Example 2: how to make a gameobject follow another object's path
void Update()
{
transform. position = Vector3. Lerp(transform. position, target. position, speedPosition * Time. deltaTime);
transform. rotation = Quaternion. Slerp(transform. rotation, target. rotation, speedRotation * Time. deltaTime);
}