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 Bezari0us · Jun 12, 2018 at 10:49 AM · scripting problemgameobjectshootingraycasthit2d

Duplicate object and shooting problem (SOLVED)

I'm learning to make a 2D platformer. I have a gun rotating around the mouse pointer and shooting at the direction of the mouse pointer when the left mouse button is clicked. The flying enemy has a Box Collider 2D on it. When i use the gun to kill the original flying enemy, it worked pretty well, the flying enemy died. But after i created a duplicate of my original flying enemy, something weird happened. If i shoot at the duplicate one, it would die like normal. But if i shoot the original one first, the duplicate one will be taken damage and will die first then if i keep shooting at it, the original one will be taken damage and die like normal. Can someone point out my problem here? Thank you for taking your time to read my question. Here is the script i attached to the gun.

 void FireRocket()
     {
         Vector2 mouPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
 
         Vector2 gunTipPosition = new Vector2(gunTip.position.x, gunTip.position.y);
 
         RaycastHit2D hit = Physics2D.Raycast(gunTipPosition, mouPos - gunTipPosition);
 if (hit.collider != null)
         { 
             if(hit.collider.tag=="Flying Enemies")
             {
                   FindObjectOfType<MakeDead>().RemoveHealth(damage);
             }
         }
 }

And here is the scrip i attached to the flying enemy

 void Start () {
         currentHp = maxHp;
 }
 
 public void RemoveHealth(float amount)
     {
         currentHp -= amount;
         healthBar.fillAmount = currentHp / maxHp;
         if (currentHp <= 0 && !hasDied)
         {
             hasDied = true;
             Destroy(gameObject);
             Instantiate(bloodParticle, transform.position, transform.rotation);
         }
     }




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 bakir-omarov · Jun 12, 2018 at 11:05 AM

See, FindObjectOfType() will look for all GameObjects on scene, and first finded GameObject with MakeDead script will your find result.
It is wrong, because you need to kill/damage your target enemy. You can find your target enemy game object from your hit, by just using hit.collider.gameObject . And then just find in this game object your MakeDead script using GetComponent(), and call function RemoveHealth(damage);.

P.S i also cleared your code, please check if its works, because i don't tested it.

Just change your code to it:


     void FireRocket()
     {
         Vector2 mousPos= Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         RaycastHit2D hit = Physics2D.Raycast(gunTip.position, mouPos - gunTip.position);
         if (hit.collider != null)
         {
             if (hit.collider.tag == "Flying Enemies")
             {
                 hit.collider.gameObject.GetComponent<MakeDead>().RemoveHealth(damage);
             }
         }
     }











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 Bezari0us · Jun 12, 2018 at 11:11 AM 0
Share

Wow, worked like charm. Thank you very much

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

181 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Why are these object passing through each other? 1 Answer

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer

Shooting with get joint rotation .eulerAngles kinect 0 Answers

Update list on mouse click 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