x


unity 3d enemy that shoots at you and gets killed

how do i make an enemy shoot at the player and if the player shoots at it. it'll die within 20 bullets.

more ▼

asked May 09 '11 at 06:07 PM

bob 7 gravatar image

bob 7
-4 8 8 15

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

5 answers: sort voted first

In basic it is pretty easy.

First you will need to make the script that stores the health and can be accessed by another scripts, so:

Add this to the enemy and name the script enemyHealth:

  static var health = 100; //Or any other value.

//The you want to declare when the character dies and what will happen if he dose:

    function Update ()
    {
        if(health <= 0)
        {
        // Let the character die
        }
    }

Then we would want to create a new script that hurts the enemy:

Put this on the camera:

var hit : RaycastHit;
var damage = 10;

function Update ()
{
Physics.Raycast(transform.position, transform.forward, hit, Mathf.Infinity);

if(hit.collider.tag == "enemy" && Input.GetButtonDown("Fire1")) //Add a tag to the player that is named "enemy"
    {
    enemyHealth.health -= damage;
    }
}

This is untested so there may be errors.

more ▼

answered May 09 '11 at 06:51 PM

OrangeLightning gravatar image

OrangeLightning
5.4k 47 57 112

do i add both the scripts to the character or the enemy?

May 10 '11 at 01:53 PM bob 7

how do i add a Raycast?

May 10 '11 at 01:56 PM bob 7

there comes an error which is as followed "(10,5): BCE0005: Unknown identifier: 'EnemyHealth'.s"

May 10 '11 at 05:05 PM bob 7

The script name and the name of the script inside the script HAS to be the same or else it wont work.

May 10 '11 at 05:57 PM OrangeLightning

thank you ill test it out and if it works ill rate u. professnal orangelightning. :). gd boy

May 10 '11 at 07:23 PM bob 7
(comments are locked)
10|3000 characters needed characters left

go check the fps tutorial. it already features such a simple AI, modify health and bullet damage values to get to your 20 hits.

http://unity3d.com/support/resources/tutorials/fpstutorial.html

more ▼

answered May 09 '11 at 06:47 PM

red 2 gravatar image

red 2
77 1 1 9

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

For the enemy health just put Var Enemy health = 10;

more ▼

answered May 21 '12 at 04:11 PM

keshawn gravatar image

keshawn
0

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

for the enemyHealth just put (var enemyHealth = 10;

more ▼

answered May 21 '12 at 04:11 PM

keshawn gravatar image

keshawn
0

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

I am having this same type of issue and the fps tutorial sounds like it no longer works with the new unity. Is there somewhere else to try and look? Or if someone is just willing to listen to my new issue and help me out that would be cool too.

more ▼

answered Feb 17 '12 at 07:24 PM

jybrd gravatar image

jybrd
16 1 2 5

(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:

x651
x383
x70
x63

asked: May 09 '11 at 06:07 PM

Seen: 2623 times

Last Updated: May 21 '12 at 04:11 PM