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 /
  • Help Room /
avatar image
0
Question by MadAce · Oct 20, 2015 at 07:01 PM · instantiateraycastdamageshotgun

Problem - Shotgun raycast instantiates multiple destroyed gameobjects instead of one

Hello again!

I was following PushyPixels' tutorial on making a shotgun using multiple raycasts. Everything works except for one problem. I have a wooden box that has health script on it, so when box' health drops to 0, the game object is deleted and destroyed version of a box is instantiated as well as some particle effects. Now the problem:

If I shoot just one raycast that does enough damage to destroy the box - no problem. If I shoot two raycasts and both of them hit the box then the number of instantiated destroyed objects will be doubled. If I use three then it will be tripled and so on....

This is script that shoots raycasts:

public class ShotgunWeapon : BaseWeapon { public int shotFragments = 8; public float spreadAngle = 10.0f; public float BulletForce = 10.0f;

 protected override void PrimaryFire()
 {
     for (int i = 0; i < shotFragments; i++) {
         RaycastHit hit;

         Quaternion fireRotation = Quaternion.LookRotation(transform.forward);

         Quaternion randomRotation = Random.rotation;


         fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0.0f, spreadAngle));

         if (Physics.Raycast(transform.position, fireRotation * Vector3.forward, out hit, Mathf.Infinity, layerMask))
         {
             GunHit gunHit = new GunHit();
             gunHit.damage = damage;
             gunHit.raycastHit = hit;

            //Check if hit object has health
             HasHealth h = hit.collider.gameObject.GetComponent<HasHealth>();

             if (h != null)
             {
                 h.ReceiveDamage(damage);
             }

             if (gunHit.raycastHit.rigidbody)
             {
                 gunHit.raycastHit.rigidbody.AddForceAtPosition(transform.forward * BulletForce * 10, gunHit.raycastHit.point);
             }

         }
     }
 }

}

It's inherented class, so this is the base weapon script:

public abstract class BaseWeapon : MonoBehaviour {

 public float fireDelay = 0.1f;
 public int maxAmmo = 0;
 public float damage = 1.0f;
 public string primaryFire = "Fire1";
 public LayerMask layerMask = -1;
 public bool automaticFire = false;

 private bool readyToFire = true;
 private int currentAmmo;

 protected abstract void PrimaryFire();

 void start()
 {
     currentAmmo = maxAmmo;
 }

 // Update is called once per frame
 void Update()
 {
     CheckInput();
 }

 protected virtual void CheckInput()
 {
     bool primaryFirePressed;

     if (automaticFire)
     {
         primaryFirePressed = Input.GetButton(primaryFire);
     }
     else
     {
         primaryFirePressed = Input.GetButtonDown(primaryFire);
     }

     if (primaryFirePressed)
     {
         if (readyToFire && (currentAmmo > 0 || maxAmmo == 0))
         {
             PrimaryFire();
             readyToFire = false;
             currentAmmo--;
             Invoke("SetReadyToFire", fireDelay);
         }
     }
 }
 void SetReadyToFire()
 {
     readyToFire = true;
 }

}

And finally this is the script for health:

public class HasHealth : MonoBehaviour {

 public float HitPoints = 10f;
 public GameObject DestroyedParticle;
 public GameObject DestroyedObject;

 public void ReceiveDamage (float amount)
 {
     HitPoints -= amount;
     if (HitPoints <= 0)
     {
         Die();
     }
 }
 void Die()
 {
     Destroy(gameObject);
     Instantiate(DestroyedObject, transform.position, transform.rotation);
     Instantiate(DestroyedParticle, transform.position, transform.rotation);           
 }

}

I'm pretty sure the problem is in rays that hit the box in the same time and they all trigger the event that instantiantes the destroyed box object. But how to solve it?

Sorry for the wall of text, but that's been bugging me for quite a while.

Thanks in advance!

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 MadAce · Oct 21, 2015 at 07:08 AM

Managed to fix the problem thanks to a collegue at work!

Problem was in HasHealth script. Fixed script:

      public void ReceiveDamage (float amount)
         {                    
             if (HitPoints > 0)
             {
                 HitPoints -= amount;
                 Debug.Log(HitPoints);       
                 if (HitPoints <= 0)
                 {
                     Die();
                 }
             }
         }      
         void Die()
         {
             Destroy(gameObject);
             Instantiate(DestroyedObject, transform.position, transform.rotation);
             Instantiate(DestroyedParticle, transform.position, transform.rotation);           
         }
 }



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 Gigoorin · May 25, 2019 at 09:26 PM 0
Share

I had the exact same issue. Thanks to you it's sorted :D I was just missing the "if (HitPoints > 0)".

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

cannot reference parent network identity.netId when applying damage 1 Answer

Enemy is shooting the opposite direction of player 1 Answer

FPS Raycasting vs Instantiate 0 Answers

Physics.Raycast ignores instantiated objects. 1 Answer

How do you make bullet Spread? 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