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
1
Question by SniperED007 · Sep 28, 2013 at 07:30 PM · shootingscorebullets

How to detect which player shot the bullet?

A player shoots a bullet:

 if (Input.GetButtonDown("Fire1"))
         {
             Rigidbody bulletInstance;
             bulletInstance = Instantiate(Bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
             bulletInstance.AddForce(barrelEnd.forward * Velocity);    
         }

And then when my bullet hits the enemy I want to know which players bullet it was that shot the bullet. This is in my Bullet class:

 void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.tag == "Enemy")
     {
         collision.gameObject.SendMessage("Hit");
         Destroy(gameObject);
     }
 }

Basically what I want to do is to be able add to the score of the player who shot the bullet. Any ideas?

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

Answer by aldonaletto · Sep 28, 2013 at 07:40 PM

You should place a reference to the shooter in the Bullet script when shooting - like this (supposing that the bullet script is called Bullet.cs):

 if (Input.GetButtonDown("Fire1"))
        {
          Rigidbody bulletInstance;
          bulletInstance = Instantiate(Bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
          bulletInstance.AddForce(barrelEnd.forward * Velocity); 
          // set the shooter variable in the bullet script:
          bulletInstance.GetComponent<Bullet>().shooter = transform;
        }

Obviously, you should declare a public Transform variable shooter in the bullet script

 public Transform shooter;
 
 void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.tag == "Enemy")
     {
        collision.gameObject.SendMessage("Hit");
        Destroy(gameObject);
        shooter.SendMessage("AddToScore", 5); // add 5 points to the shooter's score
     }
 }

And some script attached to the player should have the function AddToScore that would effectively increment the score:

 void AddToScore(int points){
     score += points;
 }
Comment
Add comment · Show 3 · 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 arjunksajsab · Jul 19, 2017 at 05:50 AM 0
Share

hello, i would like to know whether this is possible in the case where we instantiate bullet in a [command](Cmdshoot())??

avatar image stjernerlever · Jul 18, 2019 at 09:10 AM 0
Share

But you never set your shooter equal to anything? You just say there is a public transform shooter. Since the bullet is spawned, you cannot assign it in the inspector. So how do you assign it during runtime?

avatar image Hellium stjernerlever · Jul 18, 2019 at 09:30 AM 0
Share

You will find the answer in the provided code....

       bulletInstance.GetComponent<Bullet>().shooter = transform;

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why will the bullets sometimes not show up in the game view when the player shoots? 0 Answers

How can I match the score script to my fire script, on a per click basis 1 Answer

How can I make bullets shoot out of a plane? 1 Answer

Survival Shooter Tutorial: Why Isn't the Score Increasing When I Shoot an Enemy Down? 0 Answers

RayCast Shooting And Errors 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