• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-2
Question by bob 7 · May 09, 2011 at 06:07 PM · enemykillbce0005health

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FLASHDENMARK · May 09, 2011 at 06:51 PM

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bob 7 · May 10, 2011 at 01:53 PM 0
Share

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

avatar image bob 7 · May 10, 2011 at 01:56 PM 0
Share

how do i add a Raycast?

avatar image bob 7 · May 10, 2011 at 05:05 PM 0
Share

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

avatar image FLASHDENMARK · May 10, 2011 at 05:57 PM 0
Share

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

avatar image bob 7 · May 10, 2011 at 07:23 PM 0
Share

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

avatar image
0

Answer by red 2 · May 09, 2011 at 06:47 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jybrd · Feb 17, 2012 at 07:24 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by keshawn · May 21, 2012 at 04:11 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by keshawn · May 21, 2012 at 04:11 PM

for the enemyHealth just put (var enemyHealth = 10;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to create an NPC enemy which can take damage and be killed when its energy or health reaches zero? 2 Answers

Need help for programming the killing of an enemy 1 Answer

If Enemy Health (< 10) then Exp (+10)? 1 Answer

Int value only changes after I stop playing 1 Answer

how can i do a headshot with a rigidbody bullet? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges