x


Kill Enemy when shot 4 times

How do I kill an enemy in Unity. I want to shoot a torrent 4 times before it is destroyed. Here is my code for the prefab playshot and I have it attached to my player.

var projectile : Rigidbody; var range = 100.0; var speed = 20; function Update () { if (Input.GetButtonDown ("Fire1")) { var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation); instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed)); Physics.IgnoreCollision(instantiatedProjectile.col lider, transform.root.collider); } }

What type of code do I attach to the enemy?

code for the enemy turrent.

var LookAtTarget:Transform; var damp = 4.0; var bulletPrefab:Transform; var savedTime=0;

function Update () { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

var seconds : int = Time.time; var oddeven = (seconds % 2);

if(oddeven) { Shoot(seconds); } } } function Shoot(seconds) { if(seconds!=savedTime) { var bullet = Instantiate(bulletPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity); bullet.rigidbody.AddForce(transform.forward * 3000); savedTime=seconds; } }

more ▼

asked Dec 16 '10 at 01:34 AM

Rodney Rogers gravatar image

Rodney Rogers
1 1 1 1

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

3 answers: sort voted first

//On enemy var timesHit = 0;

var bullets = GameObject.FindGameObjectsWithTag ( "bullet" );

for ( var i = 0 ; i < bullets.length ; i++ ) { distance = bullets[];

if ( distance <= 1 )
{
    timesHit++;
}

if ( timesHit >= 4 )
{
    Destroy ( GameObject );
}

}

This the error message I am getting, Assets/Standard Assets/Scripts/EnemyHits.js(9,23): UCE0001: ';' expected. Insert a semicolon at the end.

more ▼

answered Dec 16 '10 at 09:57 AM

Rodney Rogers 1 gravatar image

Rodney Rogers 1
1

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

var life = 0;

function OnCollisionEnter(boom : Collision) {

if(boom.gameObject.tag == "bullet")
{

        life +=1;
        if(life == **5**)
        {

            Destroy(gameObject);

        }

}

}

just paste this code to your enemy, change the value of highlighted number in IF statement depends on you how many shot you want to destroy it.

more ▼

answered Mar 10 '11 at 12:44 PM

user-9449 (yahoo) gravatar image

user-9449 (yahoo)
1

The script works great just take away the ** (I don't know why you put that)

Jul 29 '12 at 04:23 PM BigBlob
(comments are locked)
10|3000 characters needed characters left
var projectile : Rigidbody;
var range = 100.0;
var speed = 20;
function Update ()
{
    if ( Input.GetButtonDown ( "Fire1" ) )
    {
        var instantiatedProjectile : Rigidbody = Instantiate ( projectile, transform.position, transform.rotation );
        instantiatedProjectile.velocity = transform.TransformDirection ( Vector3 ( 0, 0, speed ) );

        Physics.IgnoreCollision ( instantiatedProjectile.col lider, transform.root.collider );
    }
}

var LookAtTarget: Transform;

var damp = 4.0;

var bulletPrefab: Transform;

var savedTime = 0;

function Update ()
{
    if ( LookAtTarget )
    {
        var rotate = Quaternion.LookRotation ( LookAtTarget.position - transform.position );

        transform.rotation = Quaternion.Slerp ( transform.rotation, rotate, Time.deltaTime * damp );

        var seconds : int = Time.time;

        var oddeven = ( seconds % 2 );

        if ( oddeven )
        {
            Shoot ( seconds );

        }
    }
}

function Shoot ( seconds )
{
    if ( seconds != savedTime )
    {
        var bullet = Instantiate ( bulletPrefab, transform.Find ( "spawnPoint" ).transform.position, Quaternion.identity );

        bullet.rigidbody.AddForce ( transform.forward * 3000 ); savedTime = seconds;
    }
}

Formatted...

My solution:

Make a script on the enemy, have it so that it has a timesHit var.

Have the same script find the bullets (Whether through tags or by names),.

Check the distance between the bullets and the enemy.

Then, If it hits, make timesHit--, and then if timesHit == 0, than Destroy(gameObject);... Just my preference...

//On enemy
var timesHit = 0;

var bullets = FindGameObjectsWithTag ( "bullet" );

for ( var i = 0 ; i < bullets.length ; i++ )
{
    distance = *PSUEDO CODE: bullets[i] to Enemy ( IF MULTIPLE ENEMIES, MAKE IT AN ARRAY ) *;

    if ( distance <= 1 )
    {
        timesHit++
    }

    if ( timesHit >= 4 )
    {
        Destroy ( GameObject );
    }
}
more ▼

answered Dec 16 '10 at 01:40 AM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

Thank you Justin. I will try it right now.

Dec 16 '10 at 01:44 AM Rodney Rogers

Do I attach it to my player or enemy?

Dec 16 '10 at 01:44 AM Rodney Rogers

Rodney, it's your code just neat so that other people can help... It's not changed by any means... I'll edit my answer for what I'd do personally...

Dec 16 '10 at 02:05 AM Justin Warner

Thanks for your help.

Dec 16 '10 at 02:49 AM Rodney Rogers

var timesHit = 0;

function Update () {

}

do I add if(hits < 4) { var timeHit = 4 timesHit--; }

Is is that correct.

Dec 16 '10 at 03:57 AM Rodney Rogers
(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:

x672
x64

asked: Dec 16 '10 at 01:34 AM

Seen: 2150 times

Last Updated: Jul 29 '12 at 04:23 PM