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 Jarrod_Walker · Oct 31, 2011 at 10:39 PM · aidamage

AI Damage if touched

How can you make an AI character cause damage to the player if it touches it.

If needed my health script is:

var healthdead : Texture2D; //dead var health1 : Texture2D; //one hit left var health2 : Texture2D; //two hits left var health3 : Texture2D; //three hits left var health4 : Texture2D; //four hits left var health5 : Texture2D; //five hits left var health6 : Texture2D; //six hits left var health7 : Texture2D; //seven hits left var health8 : Texture2D; //eight hits left var health9 : Texture2D; //nine hits left var health10 : Texture2D; //ten hits left var health11 : Texture2D; //eleven hits left var health12 : Texture2D; //twelve hits left var health13 : Texture2D; //thirteen hits left var health14 : Texture2D; //fourteen hits left var health15 : Texture2D; //fifteen hits left var health16 : Texture2D; //sixteen hits left var health17 : Texture2D; //seventeen hits left var health18 : Texture2D; //eighteen hits left var health19 : Texture2D; //nineteen hits left var health20 : Texture2D; //twenty hits left var health21 : Texture2D; //twenty one hits left var health22 : Texture2D; //twenty two hits left var health23 : Texture2D; //twenty three hits left var health24 : Texture2D; //full health

static var LIVES = 24;

function Update () { switch(LIVES) { case 24: guiTexture.texture = health24; break;

     case 23:
         guiTexture.texture = health23;
     break;
         
     case 22:
         guiTexture.texture = health22;
     break;
         
     case 21:
         guiTexture.texture = health21;
     break;
         
     case 20:
         guiTexture.texture = health20;
     break;
     
     case 19:
         guiTexture.texture = health19;
     break;
     
     case 18:
         guiTexture.texture = health18;
     break;
     
     case 17:
         guiTexture.texture = health17;
     break;
         
     case 16:
         guiTexture.texture = health16;
     break;
     
     case 15:
         guiTexture.texture = health15;
     break;
         
     case 14:
         guiTexture.texture = health14;
     break;
         
     case 13:
         guiTexture.texture = health13;
     break;
         
     case 12:
         guiTexture.texture = health12;
     break;
         
     case 11:
         guiTexture.texture = health11;
     break;
     
     case 10:
         guiTexture.texture = health10;
     break;
         
     case 9:
         guiTexture.texture = health9;
     break;
         
     case 8:
         guiTexture.texture = health8;
     break;
         
     case 7:
         guiTexture.texture = health7;
     break;
         
     case 6:
         guiTexture.texture = health6;
     break;
         
     case 5:
         guiTexture.texture = health5;    
     break;    
         
     case 4:
         guiTexture.texture = health4;
     break;    
         
     case 3:
         guiTexture.texture = health3;
     break;
             
     case 2:
         guiTexture.texture = health2;
     break;
     
     case 1:
         guiTexture.texture = health1;
     break;
         
     case 0:
         //gameover script here
     break;
 }

}

Comment
Add comment · Show 1
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 GoSuNeem · Nov 01, 2011 at 03:53 AM 3
Share

ohh man... just a tip... For those texture variables, you might just want to make a array of textures. then just do

guiTexture.texture = health[LIVES];

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Nov 01, 2011 at 12:11 AM

You could use a trigger object childed to the enemy; when the player enters this trigger, you can reduce its health in the player script, since OnTrigger events are sent to both, the trigger and the object that entered it.
Child an object of suitable shape (cube, sphere, capsule etc.) to the enemy, adjust its dimensions, disable the Mesh Renderer to make it invisible, set Is Trigger true and tag this object as "Enemy" (for instance). In the player's script, add the code below:

function OnTriggerEnter(col: Collider){
    if (col.tag == "Enemy"){ // verify if it's the enemy trigger
        HealthScript.LIVES--; // supposing the script above is HealthScript.js
    }
}
Comment
Add comment · Show 6 · 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 Jarrod_Walker · Nov 01, 2011 at 12:24 AM 0
Share

The thing is tho which i forgot to mention is that the ai are spawned by an AI$$anonymous$$anager and AISpawner, so i dont have any access to the AI themselves

avatar image aldonaletto · Nov 01, 2011 at 03:03 AM 0
Share

Well, if the enemies are CharacterControllers or have rigidbodies, you can do the inverse thing: add the trigger to the player. The function will work the same, provided that the enemies all have a common tag like "Enemy" or equivalent (use the correct tag in this script).

avatar image Jarrod_Walker · Nov 01, 2011 at 05:57 AM 0
Share

Thanks mate

avatar image Jarrod_Walker · Nov 01, 2011 at 07:31 AM 0
Share

Its still not working. I have put the script in and it doesn't work it says:

function OnTriggerEnter(col: Collider){ if (col.tag == "EnemyTentacles"){ // verify if it's the enemy trigger HealthBar.LIVES--; // supposing the script above is HealthScript.js } } EnemyTentacles is the name of the enemy I have also created that capsule around the player and called it damage i tryed that in the script to and it didn't work either

avatar image aldonaletto · Nov 01, 2011 at 09:57 AM 0
Share

EnemyTentacles is the enemy name or tag? If it's the name, compare col.name ins$$anonymous$$d. You can also place a debug line to help understand what is actually happening:

 function OnTriggerEnter(col: Collider){
     print("Hit by "+col.name); // tell what hit the player
     if (col.name == "EnemyTentacles"){
         HealthBar.LIVES--;
     }
 }
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Attack Script problem please help 1 Answer

Player Damage to enemy damage melee system 0 Answers

AI keeps attacking objects that are not player. 1 Answer

Ai that applies damage in collision? 1 Answer

Ai that applies damage when in range? 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