PhysicsCollision.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. protected override void OnValidate()
  11. {
  12. base.OnValidate();
  13. if (rigidbody3D == null)
  14. rigidbody3D = GetComponent<Rigidbody>();
  15. }
  16. void Start()
  17. {
  18. rigidbody3D.isKinematic = !isServer;
  19. }
  20. [ServerCallback]
  21. void OnCollisionStay(Collision other)
  22. {
  23. if (other.gameObject.CompareTag("Player"))
  24. {
  25. // get direction from which player is contacting object
  26. Vector3 direction = other.contacts[0].normal;
  27. // zero the y and normalize so we don't shove this through the floor or launch this over the wall
  28. direction.y = 0;
  29. direction = direction.normalized;
  30. // push this away from player...a bit less force for host player
  31. if (other.gameObject.GetComponent<NetworkIdentity>().connectionToClient.connectionId == NetworkConnection.LocalConnectionId)
  32. rigidbody3D.AddForce(direction * force * .5f);
  33. else
  34. rigidbody3D.AddForce(direction * force);
  35. }
  36. }
  37. }
  38. }