x


Ai Zombie Attack Help Script

OK so i'm making a zombie game and so far i have a zombie that will walk to me and i move it will turn but once it gets relly close it stops moving and won't start again? also how do i make it so when it's close to me lik a raange of 5, or 6 it will cause me 50 points damage after every 1.5 seconds it will damage again. help would be apriciatexd ohh sorry it moves because i put a waypoint atached to me so they follow the waypoint but it just stops when they get close to me?

more ▼

asked Jul 15 '10 at 04:52 PM

user-3180 (yahoo) gravatar image

user-3180 (yahoo)
164 9 9 16

Post the zombie script so we can see

Dec 07 '10 at 07:39 PM Rennat
(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

Here's one script I used for that sort of problem.

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;
private var attackTime = Time.time;

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 () {

    // 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;

        // 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) {

                    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.

        // start chasing if target comes close enough
        if (distance < chaseThreshold) {
            chasing = true;
        }
    }

}

function OnTriggerEnter (other: Collider) {
if (other.gameObject.CompareTag("Bullet")){
chaseThreshold=100000000;
}
}

it was pretty useful to get me going. should be pretty self explanatory. just comment if you have any questions

more ▼

answered Apr 17 '11 at 03:16 AM

cidmodder gravatar image

cidmodder
191 47 56 69

Hey can i use this code to publish in my game? I am adding a game to the Mac app store and i need some code for the zombie? If you do allow i can give you a free copy... :)

Thanks,

Sam

May 10 '12 at 09:41 PM samdogg7

i dont have health script so i dosent work very good anyone has health script that could work with this attack script?

Jun 03 '12 at 11:19 AM GRZN
(comments are locked)
10|3000 characters needed characters left

I think (just a thought) thatwhen the zombie reaches you, he reaches his destination. mission over. and then, he does nothing.

more ▼

answered Oct 07 '10 at 11:41 PM

kroltan bahuman gravatar image

kroltan bahuman
60 1 2 8

(comments are locked)
10|3000 characters needed characters left

try this raycast; Physics.RayCast(transform.position,player.position,10); if (hit.distance<30) { //attack code here }

more ▼

answered Dec 07 '10 at 06:14 PM

err gravatar image

err
16

(comments are locked)
10|3000 characters needed characters left

I would love some help that script works but i dont have a health script so? how do i actually get hit by the zombie? could anyone send me a health script if someone has worked that out would give you credits :)

more ▼

answered Jun 03 '12 at 01:18 PM

GRZN gravatar image

GRZN
0

(comments are locked)
10|3000 characters needed characters left

The problem is that you set moveSpeed to 0 when the zombie reaches melee range without setting it back to 3 when you are away again.

more ▼

answered May 10 '12 at 09:59 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3314
x954
x122
x81

asked: Jul 15 '10 at 04:52 PM

Seen: 5242 times

Last Updated: Jun 03 '12 at 01:18 PM