Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Aydan · Apr 19, 2011 at 10:41 AM · instantiateshootingbulletenemydamage

Enemy Damage problem

Hello

Im having trouble with my script I want it so when my enemys health = 0 it dies and Instantiate an exp object and the enemy dies with an explosion but when it hits the player it explodes takes life off the player but doesnt give exp heres my script

var maxHealth = 100; var curHealth = 100; var Playerdamage = 30; var Enemydamage = 10; var target : GameObject; var explosion : GameObject; var exp : GameObject;

function Update () { AddjustCurrentHealthEnemy(0); }

function AddjustCurrentHealthEnemy(adjE) { curHealth += adjE;

 if(curHealth < 0)
     curHealth = 0;

 if (curHealth > maxHealth)
     curHealth = maxHealth;

 if(maxHealth < 1)
     maxHealth = 1;

 if(curHealth < 1) { //die
     var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
     var newExp:GameObject=Instantiate(exp,transform.position,transform.rotation);
     Destroy(gameObject);
 }

}

function OnCollisionEnter(collision : Collision){ if(collision.gameObject.tag == "Bullet"){ curHealth -= Playerdamage; }

 if(collision.gameObject.Find("Player")){
     target.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-Enemydamage);
     var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
     Destroy(gameObject);
 }

}

this works for when the enemy hits the player but when i shoot the enemy and it dies i loose health and there no exp. heres my other script

var maxHealth = 100; var curHealth = 100; var Playerdamage = 30; var Enemydamage = 10; var target : GameObject; var explosion : GameObject; var exp : GameObject;

function Update () { AddjustCurrentHealthEnemy(0); }

function AddjustCurrentHealthEnemy(adjE) { curHealth += adjE;

 if(curHealth < 0)
     curHealth = 0;

 if (curHealth > maxHealth)
     curHealth = maxHealth;

 if(maxHealth < 1)
     maxHealth = 1;

 if(curHealth < 1) { //die
     var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
     var newExp:GameObject=Instantiate(exp,transform.position,transform.rotation);
     Destroy(gameObject);
 }

}

function OnCollisionEnter(collision : Collision){ if(collision.gameObject.tag == "Bullet"){ curHealth -= Playerdamage; }

 if(collision.gameObject.tag == ("Player")){
     target.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-Enemydamage);
     var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
     Destroy(gameObject);
 }

}

this works for when i shoot the enemy it dies i DONT loose health and the exp object appears. but all im changing is

if(collision.gameObject.Find("Player"))

to

if(collision.gameObject.tag == ("Player"))

im really confused can someone please help

Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Apr 19, 2011 at 03:33 PM

if(collision.gameObject.tag == "Bullet") checks the TAG (from the tag dropDown) of whatever you hit. Maybe its actual name is blasterBolt or creamPie, but it has a Bullet tag.

Find is for looking for other gameObjects in the Inspector, by name. For example, in Start you could put target=GameObject.Find("Player"));. It does the same as dragging the Player into target in the Inspector.

if(collision.gameObject.Find("Player")) isn't completely legal. It probably gets turned into big-GameObject Find. So, whenever the enemy hits anything, it checks whether a player exists anywhere in the game, and hurts him.

Comment
Add comment · Show 1 · 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 Aydan · Apr 19, 2011 at 09:18 PM 0
Share

ok thanks ill try to fix this

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Char shots oneside only / kills enemy on specific place. 0 Answers

need help with instantiating for making a bullet shoot 1 Answer

Bullet stops when new bullet instantiates pls help 3 Answers

Control amount of bullets 2 Answers

Invoke is not working properly. 0 Answers


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