PhysicsCollision.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. namespace Mirror.Examples.MultipleAdditiveScenes
  3. {
  4. [RequireComponent(typeof(Rigidbody))]
  5. public class PhysicsCollision : NetworkBehaviour
  6. {
  7. [Tooltip("how forcefully to push this object")]
  8. public float force = 12;
  9. public Rigidbody rigidbody3D;
  10. void OnValidate()
  11. {
  12. if (rigidbody3D == null)
  13. rigidbody3D = GetComponent<Rigidbody>();
  14. }
  15. void Start()
  16. {
  17. rigidbody3D.isKinematic = !isServer;
  18. }
  19. [ServerCallback]
  20. void OnCollisionStay(Collision other)
  21. {
  22. if (other.gameObject.CompareTag("Player"))
  23. {
  24. // get direction from which player is contacting object
  25. Vector3 direction = other.contacts[0].normal;
  26. // zero the y and normalize so we don't shove this through the floor or launch this over the wall
  27. direction.y = 0;
  28. direction = direction.normalized;
  29. // push this away from player...a bit less force for host player
  30. if (other.gameObject.GetComponent<NetworkIdentity>().connectionToClient.connectionId == NetworkConnection.LocalConnectionId)
  31. rigidbody3D.AddForce(direction * force * .5f);
  32. else
  33. rigidbody3D.AddForce(direction * force);
  34. }
  35. }
  36. }
  37. }