Answers for "unity follow object"

C#
0

how to make an object follow the player in unity

Add this to the zombie(s):

 //You may consider adding a rigid body to the zombie for accurate physics simulation
 private GameObject wayPoint;
 private Vector3 wayPointPos;
 //This will be the zombie's speed. Adjust as necessary.
 private float speed = 6.0f;
 void Start ()
 {
      //At the start of the game, the zombies will find the gameobject called wayPoint.
      wayPoint = GameObject.Find("wayPoint");
 }
 
 void Update ()
 {
      wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
      //Here, the zombie's will follow the waypoint.
      transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
 }
 
//Add this to the player

 //In the editor, add your wayPoint gameobject to the script.
 public GameObject wayPoint;
 //This is how often your waypoint's position will update to the player's position
 private float timer = 0.5f;
 
 void Update ()
 {
      if(timer > 0)
      {
           timer -= Time.deltaTime;
      }
      if(timer <= 0)
      {
           //The position of the waypoint will update to the player's position
           UpdatePosition();
           timer = 0.5f;
      }
 }
 
 void UpdatePosition()
 {
      //The wayPoint's position will now be the player's current position.
      wayPoint.transform.position = transform.position;
      
      
      //Now, create an empty gameobject and make its position equal to the player's position, however, do NOT parent it. Name the empty gameobject "wayPoint". Within the editor, add the waypoint to the player. Be aware that this is a VERY basic script.
 }
Posted by: Guest on January-11-2021
0

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);
}
Posted by: Guest on July-24-2020
0

unity follow object

public class FollowObject : MonoBehaviour
{

    public GameObject objectToFollow;

    public float speed = 2.0f;

    void Update()
    {
        float interpolation = speed * Time.deltaTime;

        Vector3 position = this.transform.position;
        position.y = Mathf.Lerp(this.transform.position.y, objectToFollow.transform.position.y, interpolation);
        position.x = Mathf.Lerp(this.transform.position.x, objectToFollow.transform.position.x, interpolation);
        position.z = Mathf.Lerp(this.transform.position.z, objectToFollow.transform.position.z, interpolation);

        this.transform.position = position;
    }
}
Posted by: Guest on August-07-2021

C# Answers by Framework

Browse Popular Code Answers by Language