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 TobiasAndrion · Jul 16, 2016 at 06:54 AM · movementphysicsnetworkingmultiplayeraddexplosionforce

Rocket Jumping Woes

I am having issues making a small network game based around rocket jumping. I am using the usual method of using addExplosionForce to affect rigidbodies attached to my characters. When I started building the controls for single player, I simply made it so my 'bullet' layer would not collide with my 'player' layer. That worked perfectly because the rocket would not hit my character's collider and then would hit the ground and apply an explosion from there. However, I ran into a problem with that strategy when converting the demo to a multiplayer game. Now I need the rocket not to hit the player who spawned it, but DO hit the other players in the arena.

I tried to have the rockets spawn a few units in front of the player, but that didn't work at all angles that i wanted to use. Then I tried to have the rocket take note of which player spawned it, and simply not apply an explosion force to that object and also not delete itself. This solution works sometimes, but introduces a bunch of other issues, and is just poor coding because it forces me to have the rocket be aware of the player and the player aware of the rocket and just bad coupling in general.

The real problem here is figuring out how to make rockets spawn really close to my face, not hit me, but do hit my buddy's face 3 ft in front of me. I have included the script that manages my rocket objects to illustrate what I mean.

 using UnityEngine;
 using System.Collections;
 
 public class RocketManager : MonoBehaviour {
 
     public float speed = 10.0f; 
     public float explosionForce = 10.0f; // value used for explosion force
     public float radius = 10.0f; // the radius of the explosion after the rocket has hit something.
     public GameObject myParent; //Used to reference the player that spawned the rocket, so it wont hit himself hopefully. not sure if this is good coding!
 
     /**
      * part of my work around. I have my rocket spawner script hand over a reference to the player
      * that spawned the object. this is obviously shitty code and doesn't work anyway.
      */
     public void setMyParent(GameObject targetParent)
     {
         myParent = targetParent;
     }
 
     // Update is called once per frame
     void Update () 
     {
         transform.position += transform.forward * speed * Time.deltaTime;
     }
 
     void OnCollisionEnter(Collision collision){
 
         Vector3 explosionPosition = transform.position;
         Collider[] colliders = Physics.OverlapSphere(explosionPosition, radius);
         foreach(Collider hit in colliders)
         {
             Rigidbody rigidBod = hit.GetComponent<Rigidbody>();
             GameObject hitObject = hit.gameObject; //part of my work around
             if(rigidBod != null && !hitObject.Equals(myParent))
             {
                 rigidBod.AddExplosionForce(explosionForce, explosionPosition, radius, 3.0f);
                 GameObject.Destroy(this.gameObject);
             }
 
 
         }
 
     }
 }
 



I can see multiple paths to fixing this and perhaps there is a standard method that people use in multiplayer games to make sure things don't collide with the player that spawned them, but I can't figure it out! Any advice would be greatly appreciated. I can include the scripts that spawn the rockets too if that would help.

Comment
Add comment · Show 3
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 AlucardJay · Jul 16, 2016 at 08:52 AM 1
Share

Perhaps you could use Physics.IgnoreCollision : https://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html

avatar image Dead_Man2001 · Jul 16, 2016 at 04:04 PM 0
Share

$$anonymous$$aybe you should set the local player's layer to one called "my player" and all the other players to one called "other players" at startup. $$anonymous$$ake is so rockets do not collide with the my player layer but do with other players.

avatar image TobiasAndrion Dead_Man2001 · Jul 18, 2016 at 09:03 PM 0
Share

Oh I like this idea. So I could have my usual "player startup" script also change the layer tag the other players? I will give both of these methods a try and post again with the results.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TobiasAndrion · Jul 19, 2016 at 03:26 AM

Thank you to @alucardj for the simple solution! Obviously, Unity has already thought this problem through for us. Unity API page on Physics.ignoreCollision

Here is the updated code for those who are interested

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ShootRocket : MonoBehaviour {
 
     public float speed = 10.0f; // how fast rocket shoots
     public GameObject rocketPrefab; //prefab of rocket object
     //Variables relating to the score ball
     public GameObject scoreBallPrefab; //The object that will be spawned when we let go of the right mouse button
     public Slider chargeSlider; // the slider that is associated with shooting the basketball.
     public float chargeSpeed = 10.0f; //how fast to fill up bar for basketball shot.
     public float maxForce = 350.0f; 
     public Transform bulletSpawn;
     public GameObject parentObject;
 
     // Update is called once per frame
     void Update () 
     {
         if( Input.GetButtonDown("Fire1"))
         {
             GameObject tempRocket = GameObject.Instantiate(rocketPrefab, 
                                    bulletSpawn.position, 
                                    Quaternion.LookRotation(transform.forward)) as GameObject;
             //Simple one line fix! Don't forget to just put in Physics. and trying to find an appropriate function
             Physics.IgnoreCollision(tempRocket.GetComponent<Collider>(), parentObject.GetComponent<CapsuleCollider>());
         }
 
         /**
          * The plan: when right click is held down, we increase the value of the slider based on time
          * when the right click is let go, we spawn a ball who will take the value as a parameter for the force
          * to go forward from the slider itself.
          */ 
         if(Input.GetButton("Fire2"))
         {
             chargeSlider.value += (chargeSpeed * Time.deltaTime);
         }
         if(Input.GetButtonUp("Fire2"))
         {
             GameObject tempBall = GameObject.Instantiate(scoreBallPrefab,
                                    bulletSpawn.position,
                                    Quaternion.LookRotation(transform.forward)) as GameObject;
             Rigidbody tempRigid = tempBall.GetComponent<Rigidbody>();
             if(tempRigid != null)
             {
                 tempRigid.AddForce(tempBall.transform.forward * chargeSlider.value * maxForce);
             }
             chargeSlider.value = 0f;
         }
     }
 }
Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Unity networking tutorial? 6 Answers

bouncing a puck of a vertical wall through code 0 Answers

Multiplayer: The more that join the slower each character is 0 Answers

Authoritative Networking 2D Movement 0 Answers

What's the best method to implement multiplayer on a Billiard game ? 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