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 DoYouSellVHS · Sep 01, 2017 at 08:24 AM · collision detectiondebuggingmultiple objects

Object hit with multiple shotgun bullets only taking damage from one bullet?

    private void OnCollisionStay(Collision collision)
     {
 
         if (collision.transform.tag == "Bullettag")
         {
             if (collision.gameObject.GetComponent<BulletScript>().playerbullet == true)
             {
                 attackedByPlayer = true;
                 player = collision.gameObject.GetComponent<BulletScript>().player;
             }
             HP -= ((collision.gameObject.GetComponent<BulletScript>().firedamage) * (1-(fireresist/100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().icedamage) * (1 - (iceresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().ballisticdamage) * (1 - (ballisticresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().bluntdamage) * (1 - (bluntresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().chaosdamage) * (1 - (chaosresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().electricdamage) * (1 - (electricresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().normaldamage) * (1 - (normalresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().poisondamage) * (1 - (poisonresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().psychedamage) * (1 - (psycheresist / 100)));
             HP -= ((collision.gameObject.GetComponent<BulletScript>().aciddamage) * (1 - (acidresist / 100)));
 }
 }


I have a script that's meant to have an enemy take damage from a bullet when they're hit by the bullet. This code works perfectly fine for individual bullets, but when I try to make a shotgun (with about 6 projectiles) the enemy is only ever hit by one bullet at a time, since its health only decrements by the damage of one bullet. How would I get around this?

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

Answer by cgarossi · Sep 01, 2017 at 08:46 AM

A way around this is to move your health decrement logic into the bullets it self. Instead of your target dealing with what hit it, move the logic into the bullet, which applies a modifier to the hit object.

This way, each bullet is responsible for dealing damage to the hit object, not the hit object working out who hit it and what damage was caused.

That way you could create a shotgun with 30 bullets firing at the same time, each individual bullet would then apply 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 Skrobie · Jun 26, 2018 at 03:44 AM 0
Share

my bullets work opposite - i have one bullet but it applies multiple damage. i have not only tried to destroy the bullet on collision but also create a toggle so that the bullet can only run the oncollision() once. Here is my script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;

public class Bullet$$anonymous$$ove : NetworkBehaviour {

 private Rigidbody rb;
 private bool hit = false;

 [SerializeField] private float setTimeTillDestroy = 5f;

 void Start() {
     rb = gameObject.GetComponent<Rigidbody>();
     rb.AddForce(transform.forward * 500, Force$$anonymous$$ode.Impulse);
     Destroy(gameObject, setTimeTillDestroy);
 }

 private void OnCollisionEnter(Collision col)
 {
     if (col.gameObject.tag == "Player")
     {
         if(hit == false)
         {
             col.gameObject.GetComponent<Health>().CmdTakeDamage(5);
             NetworkServer.Destroy(gameObject);
             hit = true;
         }
     }
 }

}

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

69 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

Related Questions

OnCollisionEnter2D and OnCollisionExit2D executed at the same time 0 Answers

Colliding with multiple objects 1 Answer

how to make an Explosive rigidbody trigger by another collision box 0 Answers

Third Person Camera Can See Through Walls! 1 Answer

OnCollisionEnter2D(coll: Collision2D) function not being triggered 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