Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 · Feb 13, 2017 at 08:11 AM · collisioninitialization

How To Check If A Bullet Hits Another Object (player) From Another Script

In my script I have a bullet initiate whenever the enemy gets close, but I need to change the PlayerHealth variable so it can't be a script on the bullet GameObject. Because of this I need to find a way to

  • Access the bullet (which should be easy)

  • check if it collides with the player

  • and if it does take off 10 health

Here is my script:

 #pragma strict
 
 var Player : Transform;
 public var MoveSpeed = 10;
 public var MaxDist = 7;
 public var MinDist = 5;
 public var AttackDistance = 5;
 public var PlayerHealth = 300;
 public var EnemyHealth = 10;
 var ani : Animator;
 var AnimAttack : boolean;
 var AnimDie : boolean;
 var AnimShoot : boolean;
 public var IsDead : boolean;
 var bullet : GameObject;
 var spawnPoint : Transform;
 
 function Start () 
 {
     ani.enabled = false;
     AnimAttack = false;
     AnimDie = false;
     IsDead = false;
     AnimShoot = false;
 }
  
 function Update () 
 {
     if(IsDead) {
         Die();
     }
     else {
         if(gameObject.tag == "EnemyMelee") {
             if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {
                 transform.LookAt(Player);
      
      
                 transform.position += transform.forward*MoveSpeed*Time.deltaTime;
  
                 if(Vector3.Distance(transform.position,Player.position) <= AttackDistance)
                 {
                     if(Vector3.Distance(transform.position,Player.position) <= AttackDistance && Input.GetMouseButtonDown(0)) {
                         EnemyHealth = EnemyHealth - 1;
                         print(EnemyHealth + " = Enemy Health");
                     }
                     else {
                         AttackMelee();
                     }
                 }
 
                 if(EnemyHealth <= 0) {
                     IsDead = true;
                 }
 
                 if(PlayerHealth <= 0) {
                     Application.LoadLevel(1);
                 }
                 if (AnimAttack == true) {
 
                     ani.SetBool("AnimAttack", true);
                     ani.SetBool("AnimDie", false);
 
                 }
                 if (AnimDie == true) {
 
                     ani.SetBool("AnimAttack", false);
                     ani.SetBool("AnimDie", true);
 
                 }
             }
 
             else {
 
             }
         }
         if(gameObject.tag == "EnemyRanged") {
             if(IsDead) {
                 Die();
             }
             else {
                 if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {
                     transform.LookAt(Player);
      
      
                     transform.position += transform.forward*MoveSpeed*Time.deltaTime;
  
                     if(Vector3.Distance(transform.position,Player.position) <= AttackDistance)
                     {
                         if(Vector3.Distance(transform.position,Player.position) <= AttackDistance && Input.GetMouseButtonDown(0)) {
                             EnemyHealth = EnemyHealth - 1;
                             print(EnemyHealth + "= Enemy Health");
                         }
                         else {
                             AttackRanged();
                         }
                     }
 
                     if(EnemyHealth <= 0) {
                         IsDead = true;
                     }
 
                     if (AnimShoot == true) {
 
                         ani.SetBool("AnimAttack", false);
                         ani.SetBool("AnimDie", false);
                         ani.SetBool("AnimShoot", true);
 
                     }
                     if (AnimDie == true) {
 
                         ani.SetBool("AnimAttack", false);
                         ani.SetBool("AnimShoot", false);
                         ani.SetBool("AnimDie", true);
 
                     }
                 }
                 else {
 
                 }
             }
         }
 
     }
 }
 
 function Die() {
 
     ani.enabled = true;
     AnimAttack = false;
     AnimDie = true;
     yield WaitForSeconds (3);
     Destroy (gameObject);
 
 }
 function AttackMelee() {
     PlayerHealth = PlayerHealth - 0.000001;
     print(PlayerHealth);
     ani.enabled = true;
     AnimAttack = true;
     AnimDie = false;
 }
 function AttackRanged() {
 
     ShootGun();
     print(PlayerHealth);
     ani.enabled = true;
     AnimAttack = false;
     AnimDie = false;
     AnimShoot = true;
 }
 function ShootGun (){
     var nBullet = Instantiate(bullet, spawnPoint.position, spawnPoint.rotation);
     nBullet.GetComponent.<Rigidbody>().velocity = spawnPoint.TransformDirection(Vector3(0,0,100));
     //Check collision here!!!
 }

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
Best Answer

Answer by · Feb 19, 2017 at 11:04 PM

I fixed it using the same method I used on every other object to detect whether the enemy the script is attached to: I checked the object's tag. I'll check the tag of the object the script is attached to, if it's tag is 'Bullet' remove 10 from the variable PlayerHealth!

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Collision detection for an initialized prefab 0 Answers

Initialising List array for use in a custom Editor 1 Answer

OnDestroy collision detect 4 Answers

Why does my player fly through my barrier? 2 Answers

Check For Collision with anything in If-else 2 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