Enemy should not move until Player is within an amount of units (Evac City scripts)

I’ve been playing around with the EVAC CITY tutorial scripts (still learning to do everything).

I wanted the enemy AI script to make the enemy only move when the Player is close by.

I found this code and put it in the void Update:

        if(distFromPlayer.magnitude < 30){
            velocity = Vector3.zero;
            target = player.position;
            velocity = (player.position - transform.position).normalized * speed;
        }
        else{
            velocity = moveDirection.normalized * speed;
        }

But it gave me 8 errors about some words being out of context. I see it would be a functional code if I nearly rewrote the existing code.

What do you think I could do, using the existing script (in the post below), to make the enemy move when the Player is near by?

Thanks

Enemy AI Code:

using UnityEngine;
using System.Collections;

public class AIscript : MonoBehaviour {
    public GameObject objPlayer;
    private GameObject objCamera;
    private VariableScript ptrScriptVariable;


    private Vector3 inputRotation;
    private Vector3 inputMovement;
    private bool meleeAttackState;
    
    //identity variables (variables specific to the game object)
    public float moveSpeed = 100f;
    public float health = 50f;
    private bool thisIsPlayer;

    private Vector3 tempVector;
    private Vector3 tempVector2;
    private int i;
    private bool meleeDamageState;

    public float animationFrameRate = 11f;
    public float walkAnimationMin = 1;
    public float walkAnimationMax = 10; 
    public float standAnimationMin = 11; 
    public float standAnimationMax = 20;
    public float meleeAnimationMin = 23; 
    public float meleeAnimationMax = 30; 
    public float spriteSheetTotalRow = 5; 
    public float spriteSheetTotalHigh = 4; 
    public float frameNumber = 1; 
    private float animationStand = 0; 
    private float animationWalk = 1; 
    private float animationMelee = 2; 
    private float currentAnimation = 1; 
    private float animationTime = 0f;
    private Vector2 spriteSheetCount; 
    private Vector2 spriteSheetOffset; 
    public GameObject objSpriteRender;
    

    void Start () {
        objPlayer = (GameObject) GameObject.FindWithTag ("Player");
        objCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
        if (gameObject.tag == "Player") { thisIsPlayer = true; }
        ptrScriptVariable = (VariableScript) objPlayer.GetComponent( typeof(VariableScript) );
    }

    void Update () {
        if (health <= 0)
        {
            removeMe();
        }
        FindInput();
        ProcessMovement();
        HandleAnimation();
        if (thisIsPlayer == true)
        {
            HandleCamera();
        }
    }

    void FindInput ()
    {
        if (thisIsPlayer == true)
        {
            FindPlayerInput();
        } else {
            FindAIinput();
        }
    }
    
        void FindPlayerInput ()
        {
            // find vector to move
            inputMovement = new Vector3( Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical") );

            // find vector to the mouse
            tempVector2 = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f); // the position of the middle of the screen
            tempVector = Input.mousePosition;
            tempVector.z = tempVector.y; 
            tempVector.y = 0;
            inputRotation = tempVector - tempVector2; // the direction we want face/aim/shoot is from the middle of the screen to where the mouse is pointing

            if ( Input.GetMouseButtonDown(0) )
            {
                HandleBullets();
            }
        }
            void HandleBullets ()
            {
                tempVector = Quaternion.AngleAxis(8f, Vector3.up) * inputRotation;
                tempVector = (transform.position + (tempVector.normalized * 0.8f));
                GameObject objCreatedBullet = (GameObject) Instantiate(ptrScriptVariable.objBullet, tempVector, Quaternion.LookRotation(inputRotation) ); // create a bullet, and rotate it based on the vector inputRotation
                Physics.IgnoreCollision(objCreatedBullet.collider, collider);
            }
        void FindAIinput ()
        {
            meleeAttackState = false;
            if (objPlayer == null)
            {
                inputMovement = Vector3.zero;
                inputRotation = transform.forward * -1;
                return;
            }
            inputMovement = objPlayer.transform.position - transform.position;
            inputRotation = inputMovement; // face the direction we are moving, towards the player
            if ( Vector3.Distance(objPlayer.transform.position,transform.position) < 1.2f )
            {
                meleeAttackState = true;
                inputMovement = Vector3.zero;
                if (currentAnimation != animationMelee) { frameNumber = meleeAnimationMin + 1; }
            }
        }
    void ProcessMovement()
    {
        tempVector = rigidbody.GetPointVelocity(transform.position) * Time.deltaTime * 1000;
        rigidbody.AddForce (-tempVector.x, -tempVector.y, -tempVector.z);

        rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
        transform.rotation = Quaternion.LookRotation(inputRotation);
        transform.eulerAngles = new Vector3(0,transform.eulerAngles.y + 180,0);
        transform.position = new Vector3(transform.position.x,0,transform.position.z);
    }
    void HandleCamera()
    {
        objCamera.transform.position = new Vector3(transform.position.x,15,transform.position.z);
        objCamera.transform.eulerAngles = new Vector3(90,0,0);
    }

    void HandleAnimation () // handles all animation
    {
        FindAnimation();
        ProcessAnimation();
    }
        void FindAnimation ()
        {
            if (meleeAttackState == true || currentAnimation == animationMelee)
            {
                currentAnimation = animationMelee;
                return;
            }
            if (inputMovement.magnitude > 0)
            {
                currentAnimation = animationWalk;
            } else {
                currentAnimation = animationStand;
            }
        }
        void ProcessAnimation ()
        {
            animationTime -= Time.deltaTime; // animationTime -= Time.deltaTime; subtract the number of seconds passed since the last frame, if the game is running at 30 frames per second the variable will subtract by 0.033 of a second (1/30)
            if (animationTime <= 0)
            {
                frameNumber += 1;
                // one play animations (play from start to finish)
                    if (currentAnimation == animationMelee)
                    {
                        frameNumber = Mathf.Clamp(frameNumber,meleeAnimationMin,meleeAnimationMax+1);
                        if (frameNumber > meleeAnimationMax)
                        {
                            meleeDamageState = false;
                            if (meleeAttackState == true)
                            {
                                frameNumber = meleeAnimationMin;
                            } else {
                                currentAnimation = animationWalk;
                                frameNumber = walkAnimationMin;
                            }
                        }

                        if (meleeAttackState == true  frameNumber > walkAnimationMin + 4  meleeDamageState == false  objPlayer != null) // if we are within 1.2 units of the player and if we are at least 4 frames into the animation, and we haven't attacked yet
                        {
                            meleeDamageState = true;
                            AIscript ptrScriptAI = (AIscript) objPlayer.GetComponent( typeof(AIscript) );
                            ptrScriptAI.health -= 10; // damage the player
                            Instantiate(ptrScriptVariable.parPlayerHit, objPlayer.transform.position, Quaternion.identity );
                        }
                    }
                // cyclic animations (cycle through the animation)
                    if (currentAnimation == animationStand)
                    {
                        frameNumber = Mathf.Clamp(frameNumber,standAnimationMin,standAnimationMax+1);
                        if (frameNumber > standAnimationMax)
                        {
                            frameNumber = standAnimationMin;
                        }
                    }
                    if (currentAnimation == animationWalk)
                    {
                        frameNumber = Mathf.Clamp(frameNumber,walkAnimationMin,walkAnimationMax+1);
                        if (frameNumber > walkAnimationMax)
                        {
                            frameNumber = walkAnimationMin;
                        }
                    }
                animationTime += (1/animationFrameRate); // if the animationFrameRate is 11, 1/11 is one eleventh of a second, that is the time we are waiting before we play the next frame.
            }
            spriteSheetCount.y = 0;
            for (i=(int)frameNumber; i > 5; i-=5) // find the number of frames down the animation is and set the y coordinate accordingly
            {
                spriteSheetCount.y += 1;
            }
            spriteSheetCount.x = i - 1; // find the X coordinate of the frame to play
            spriteSheetOffset = new Vector2(1 - (spriteSheetCount.x/spriteSheetTotalRow),1 - (spriteSheetCount.y/spriteSheetTotalHigh));  // find the X and Y coordinate of the frame to display
            objSpriteRender.renderer.material.SetTextureOffset ("_MainTex", spriteSheetOffset); // offset the texture to display the correct frame
        }
    void removeMe () // removes the character
    {
        if (thisIsPlayer == false)
        {
            Instantiate(ptrScriptVariable.parAlienDeath, transform.position, Quaternion.identity );
        } else {
            Instantiate(ptrScriptVariable.parPlayerDeath, transform.position, Quaternion.identity );
        }
        Destroy(gameObject);
    }

}

You will need the Vector3.Distance() funtion:

basically

if(Vector3.Distance(player.transform.position, enemy.transform.position) < 30) // move player if this is true

The “player” and “enemy” have to be referenced in the code. I just named them for example.

Thank you! I ended up putting this in the void AIinput():

            if (Vector3.Distance(objPlayer.transform.position, transform.position) > 10)
            {
                inputMovement = Vector3.zero;
            }

Everything works with no errors thank you! :slight_smile: