Projectile.cs 959 B

1234567891011121314151617181920212223242526272829303132333435
  1. using UnityEngine;
  2. namespace Mirror.Examples.Tanks
  3. {
  4. public class Projectile : NetworkBehaviour
  5. {
  6. public float destroyAfter = 2;
  7. public Rigidbody rigidBody;
  8. public float force = 1000;
  9. public override void OnStartServer()
  10. {
  11. Invoke(nameof(DestroySelf), destroyAfter);
  12. }
  13. // set velocity for server and client. this way we don't have to sync the
  14. // position, because both the server and the client simulate it.
  15. void Start()
  16. {
  17. rigidBody.AddForce(transform.forward * force);
  18. }
  19. // destroy for everyone on the server
  20. [Server]
  21. void DestroySelf()
  22. {
  23. NetworkServer.Destroy(gameObject);
  24. }
  25. // ServerCallback because we don't want a warning
  26. // if OnTriggerEnter is called on the client
  27. [ServerCallback]
  28. void OnTriggerEnter(Collider co) => DestroySelf();
  29. }
  30. }