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
0
Question by MadJohny · Feb 19, 2014 at 03:50 PM · c#instantiateexplosionoverlapsphererocket

Rocket Launcher explosion

Hi, for my current project I need to create a rocket launcher like explosion, the obective is that when it hits, it would make the explosion, but I'm having some problems since it doesn't react as fast as it should.

 using System.Collections;
 
 public class RocketScript : MonoBehaviour {
 
     public GameObject explosionPrefab;
     public float velocity = 7f;
     public float damage = 35f;
     public float radius = 3f;
 
     IEnumerator Start () {
         yield return new WaitForSeconds (15f);
         Destroy(gameObject);
     }
 
     void FixedUpdate () {
         rigidbody.velocity = transform.forward * velocity;
     }
 
     void OnTriggerEnter () {
         Explode();
     }
 
     void Explode () {
         Instantiate(explosionPrefab, transform.position, Quaternion.identity);
         Collider[] hitColliders = Physics.OverlapSphere(transform.position ,radius);
         foreach (Collider hitCollider in hitColliders) {
             if (hitCollider.tag == "Player") {
                 if (!Physics.Linecast(transform.position, hitCollider.transform.position)) {
                     hitCollider.SendMessageUpwards("ApplyRocketDamage", damage, SendMessageOptions.DontRequireReceiver);
                 }
             }
         }
         
         gameObject.renderer.enabled = false;
     }
 }

That is my curent script, the explosionPrefab is just a sphere (for testing purposes) with the scale of 3,3,3 what I notice is that the sphere is instantiated more to the wrong side of the wall than to the hit point, for example if it hits a thin wall, the sphere would go more to the other side of the wall than to the side of where the explosion should happen, so this makes that " if (!Physics.Linecast(transform.position, hitCollider.transform.position))" never happens, because the Linecast starts from the other side of the wall, intercepting the LineCast. What can I do to fix this?

update: Since the collision detection seemed to be somewhat difficult to get it right, I decided to completily remove the collider, and instead use a raycast to detect "collision", my fist try, with an usual raycast was working better than usual, since it was spawning where I wanted, however, sometimes the rycast didn't detect the wall and it would go straight through it, now I tried a spherecast, seems to be handling it pretty well. I hope it works

Thanks in advance.

Comment
Add comment · Show 1
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 marcfielding · Feb 19, 2014 at 04:53 PM 0
Share

If you want a nice easy way to do explosions use the Detonator Explosions pack from the asset store, its free and you just apply one of the prefabs and call explode on it from OnCollisionEnter. When your importing it DON'T fix the materials or textures when unity asks you or the explosions look messed up

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RedSandStudios · Feb 19, 2014 at 04:33 PM

The problem is that you're using transform.position as the Instantiate point for your prefab and for the Linecast:

Instantiate(explosionPrefab, transform.position`, Quaternion.identity);`

and

!Physics.Linecast(transform.position, hitCollider.transform.position)

When these are called, the transform.position of your Rocket may be through the wall already, which is why your prefab gets put on the wrong side of the wall, and why the Linecast starts on the other side of the wall.

The best way to fix this is to turn off IsTrigger on your Collider component use OnCollisionEnter instead of OnTriggerEnter. The benefit of this is that a Collision object is passed into OnCollisionEnter, which holds information about the collision, including the hit point, which is what we want in this case. So just replace OnTriggerEnter() and Explode() with:

 void OnCollisionEnter (Collision info) {
    //info.contacts[0].point is a Vector3 of the first collision point.
    Instantiate(explosionPrefab, info.contacts[0].point, Quaternion.identity);
    Collider[] hitColliders = Physics.OverlapSphere(info.contacts[0].point, radius);
    foreach (Collider hitCollider in hitColliders) {
      if (hitCollider.tag == "Player") {
       if (!Physics.Linecast(info.contacts[0].point, hitCollider.transform.position)) {
           hitCollider.SendMessageUpwards("ApplyRocketDamage", damage, SendMessageOptions.DontRequireReceiver);
       }
      }
    }
 
    gameObject.renderer.enabled = false;
 }

And also don't forget to uncheck the isTrigger tickbox on your Rocket collider. For when you change the collision prefab from a sphere to something else, you can also use the normal of the contact point to make it face the right direction, otherwise it might just face straight up and look weird:

Instantiate(explosionPrefab, info.contacts[0].point, Quaternion.Euler(info.contacts[0].normal));

Comment
Add comment · Show 4 · 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 MadJohny · Feb 19, 2014 at 05:29 PM 0
Share

Okay I'll try that in a bit once I figure out another problem I'm having right now edit: hmm it seems to be working most of the times, thanks!

avatar image MadJohny · Feb 19, 2014 at 06:01 PM 0
Share

hmm it actually seems to fail quite a lot

avatar image RedSandStudios · Feb 19, 2014 at 08:15 PM 0
Share

$$anonymous$$y apologies, it's just it seemed very similar to a problem that I was having when I was implementing rockets in my game, and this was how I solved it. What do you mean by it fails quite a lot? Does it not collide/not put the sphere prefab in the right position?

avatar image MadJohny · Feb 19, 2014 at 09:06 PM 0
Share

it still spawns after the wall a little bit, causing same problem as the trigger, I updated my question, and it seemed to work better, anyway I just gave up on rockets since this is for a simple networking project, I just wanted to learn networking better, so this problem isn't that bad at this time for me

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

OnCollisionEnter problem (RocketJump script) 1 Answer

Multiple Cars not working 1 Answer

Rocket Explosion not applying damage when hitting ground 1 Answer

Cast Source Instantiating Error 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