When object is within a radius, stop moving

by Gurdeep   Last Updated March 14, 2019 19:13 PM

I am creating a zombie game using unity, I am using the following code to make the object (zombie) move towards me, but i am trying to make it stop moving whenever it gets too close. My intention is to when it gets within a certain range it will just stop trying to move.

Here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class EnemyMove : MonoBehaviour
{

    public GameObject ThePlayer;
    public float TargetDistance;
    public float EnemyHealth;
    public float AllowedRange = 10;
    public GameObject TheEnemy;
    public float EnemySpeed;
    private Animator animation;
    public int AttackTrigger;
    public RaycastHit Shot;

    void Start()
    {
        animation = GetComponent<Animator>();

    }
    void Update()
    {
        if (PauseScript.Paused == false)
        {
            if (Input.GetButtonDown("Fire1"))
        {
            RaycastHit Shot;
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
            {
                TargetDistance = Shot.distance;
                if (TargetDistance < AllowedRange)
                {
                    if (GlobalAmmo.LoadedAmmo > 0)
                    {
                        if (EnemyHealth == 0)
                        {
                            Destroy(TheEnemy);
                        }
                        else
                        {
                            EnemyHealth = EnemyHealth - 10;
                        }
                    }
                }
            }
        }


            transform.LookAt(ThePlayer.transform);
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
            {
                EnemySpeed = 0.03f;
                animation.SetBool("Running", true);
                transform.position = Vector3.MoveTowards(transform.position, new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z), EnemySpeed);
                TargetDistance = Shot.distance;
                if (TargetDistance < AllowedRange)
                {
                    animation.SetBool("Running", false);

                    if (AttackTrigger == 0)
                    {

                        float dist = Vector3.Distance(ThePlayer.transform.position, transform.position);
                        print("Distance to other: " + dist);

                        if (dist > 3)
                        {
                            transform.position = Vector3.MoveTowards(transform.position, new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z), EnemySpeed);
                        }
                        else
                        {

                        }
                    }
                }
                else
                {
                    EnemySpeed = 0.05f;

                }
            }
        }





    }



}

Tags : unity c#


Related Questions


Updated July 12, 2015 13:05 PM

Updated July 13, 2015 13:05 PM

Updated March 18, 2016 08:05 AM