[Zombie AI] Enemy grows and pushes me when coming towards me?

Hello everyone! I’ve recently taken to creating a zombie game and I’ve found some code online to help me get started with the AI of the creatures. It works fairly well, but not perfectly. You see, when I get in range of the zombie, it turns and moves towards me (like it should) but slowly grows in size and seems to push my actual player backwards as it does so.

I’m not entirely sure, but I think it may be how I’m playing the animations in the scene?

var target : Transform; //the enemy's target

var moveSpeed = 3; //move speed

var rotationSpeed = 3; //speed of turning

var attackThreshold = 1.5; // distance within which to attack

var chaseThreshold = 10; // distance within which to start chasing

var giveUpThreshold = 20; // distance beyond which AI gives up

var attackRepeatTime = 1; // delay between attacks when within range



private var chasing = false;

public var attackTime : float;



var myTransform : Transform; //current transform data of this enemy



function Awake()

{

    myTransform = transform; //cache transform data for easy access/preformance 

    }





function Start()

{

     if (target == null && GameObject.FindWithTag("Player"))

		target = GameObject.FindWithTag("Player").transform;

}



function Update () {



    // check distance to target every frame:

    var distance = (target.position - myTransform.position).magnitude;



    if (chasing) {



        //rotate to look at the player

        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,

        Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);



        //move towards the player

        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;



		animation.Play("run");



        // give up, if too far away from target:

        if (distance > giveUpThreshold) {

            chasing = false;

        }



        // attack, if close enough, and if time is OK:

        if (distance < attackThreshold && Time.time > attackRepeatTime) {

					animation.Play("attack");

                    target.SendMessage("ApplyDamage",10);

            // Attack! (call whatever attack function you like here)

            }

        if (distance < attackThreshold) {

        moveSpeed=0;

        //stop when close enough

    }



            attackTime = Time.time+ attackRepeatTime;

        }



      else {

        // not currently chasing.

		animation.Play("idle");

        // start chasing if target comes close enough

        if (distance < chaseThreshold) {

            chasing = true;

        }

    }
}

function OnTriggerEnter (other: Collider) {

if (other.gameObject.CompareTag("Bullet")){

chaseThreshold=100000000;

}

}

Thanks for your help!

EDIT: Sorry for the odd editing, I’m not quite sure what I’ve done. Hopefully you can still read it.

Highlight everything you want to be in script format and click on the code sample button on your question editor.

1 Answer

1

Nothing seems wrong in your script. I suspect your problem may be just scale error: your character may be too little or the enemy too big, and you only notice this when he gets closer to the player (it’s a perspective camera). A good way to check this is to place the camera at some distance of the player to see what’s happening: if you’re using a first person camera, set the camera position in the Inspector to something like -2,2,-10 (10 meters behind and 2 meters above and to the left of the player) and run your game - you can compare the scales when the enemy gets close to the player.