Thursday, 15 September 2011

unity3d - Do damage to the enemy -



unity3d - Do damage to the enemy -

i have 2d, top-down game in progress.

i want able apply harm enemy when press space bar , enemy within distance of player.

at moment code moves enemy towards player , applies harm player 1 time collide. when nail space bar enemies destroyed want harm applied when enemies close player.

here enemy code currently, health scheme set-up:

var player : transform; var movespeed = 4; var mindist = 5; var health = 100; var anyvalue = 100; function update () { transform.lookat(player); if(vector2.distance(transform.position,player.position) >= mindist) { transform.position += transform.forward*movespeed*time.deltatime; } { if(health <= 0) { dead(); } } if (vector3.distance(transform.position, player.position) < somevalue && input.getkeydown(keycode.space)) { applydamage(somevalue); } } function applydamage (thedamage : int) { health -= thedamage; } function dead () { destroy (gameobject); }

i found solution.

to apply harm within distance needed this:

if (vector3.distance(transform.position, player.position) < 1.25 && input.getkeydown(keycode.space)) { applydamage(5); }

unity3d unityscript

No comments:

Post a Comment