• 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 Zwusel · May 20, 2016 at 08:00 PM · multiplayerspawnaddforce

Unet: Add a force to hit player (by projectile)

I am trying to add an explosion force to a player object by the projectile that hit him. Any suggestions on how to achieve that? It works for some rigidbodys spawned by Network.Spawn() but not for the player object.

I use this code for the Projectile:

 using UnityEngine;
 using UnityEngine.Networking;
 
 public class ShellExplosion : NetworkBehaviour
 {
     //public GameObject explosionEffect;         
     [SerializeField]
     private ParticleSystem explosionEffect;
     public float maxDamage = 100f;                    
     public float explosionForce = 1000f;             
     public float maxLifeTime = 10f;                 
     public float explosionRadius = 5f;               
 
 
     private void Start()
     {
         // If it isn't destroyed by then, destroy the shell after it's lifetime.
         Destroy(gameObject, maxLifeTime);
     }
 
 
     private void OnTriggerEnter(Collider other)
     {
         Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
 
         // Go through all the colliders...
         for (int i = 0; i < colliders.Length; i++)
         {
             // ... and find their rigidbody.
             Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody>();
 
             // If they don't have a rigidbody, go on to the next collider.
             if (!targetRigidbody)
                 continue;
 
             // Add an explosion force.
             targetRigidbody.AddExplosionForce(explosionForce, transform.position, explosionRadius);
         }
         // Unparent the particles from the shell.
         explosionEffect.transform.parent = null;
 
         // Play the particle system.
         explosionEffect.Play();
 
         // Once the particles have finished, destroy the gameobject they are on.
         Destroy(explosionEffect.gameObject, explosionEffect.duration);
 
         // Destroy the shell.
         Destroy(gameObject);
     }
 }


I could spawn the projectile with client authority but i dont know if thats a good way to do it. As it is spawned by Network.Spawn() it should be doing the AddExplosionForce on the server, right? And as i mentioned, it is working for some cubes i spawned in for testing.

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 Zwusel · May 23, 2016 at 01:26 PM 0
Share

I am an idiot. The problem had nothing to do with the code. $$anonymous$$y Player prefab just had the rigidbody on the root object and the colliders on the children. So i had to get to the root of the hit colliders and add force there.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiplayer: 4 players, each player spawning less amount of other players. 0 Answers

Unet: Spawn bullet - client does not have a reference of bullet 1 Answer

How to identify players on Photon Unity? 2 Answers

uNet Spawning Objects 0 Answers

Players spawn at same location (Unet) 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