how to make a follow script in unity

 var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform; //current transform data of this enemy function Awake() {     myTransform = transform; //cache transform data for easy access/preformance }   function Start() {      target = GameObject.FindWithTag("Player").transform; //target the player   }   function Update () {     //rotate to look at the player     var distance = Vector3.Distance(myTransform.position, target.position);     if (distance<=range2 &&  distance>=range){     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);     }         else if(distance<=range && distance>stop){      //move towards the player     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;     }     else if (distance<=stop) {     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);     }        }

3.67
6
Eddie Kal 95 points

                                    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour
{

    public float attackSpeed = 4;
    public float attackDistance;
    public float bufferDistance;
    public GameObject player;

    Transform playerTransform;

    void GetPlayerTransform()
    {
        if (player != null)
        {
            playerTransform = player.transform;
        }
        else
        {
            Debug.Log(&quot;Player not specified in Inspector&quot;);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        GetPlayerTransform();
    }

    // Update is called once per frame
    void Update()
    {
        var distance = Vector3.Distance(playerTransform.position, transform.position);
        // Debug.Log(&quot;Distance to Player&quot; + distance);
        if (distance &lt;= attackDistance)
        {
            if (distance &gt;= bufferDistance)
            {
                transform.position += transform.forward * attackSpeed * Time.deltaTime;
            }
        }
    }
}

3.67 (6 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source