RangeProjectile.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Mirror;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. public class RangeProjectile : NetworkBehaviour
  8. {
  9. public float speed;
  10. public float distance;
  11. public float lifetime;
  12. public float hitLifetime;
  13. public LayerMask isEnemy;
  14. public Vector3 direction;
  15. public uint shooterId;
  16. public float castRadius;
  17. public GameObject hitEffectVfx;
  18. public UnityEvent<enemyScript> OnHit;
  19. void Start(){
  20. // if(!isLocalPlayer){return;}
  21. Invoke("DestroyProjectile",lifetime);
  22. }
  23. public bool isLocalp;
  24. void Update(){
  25. isLocalp = isLocalPlayer;
  26. transform.Translate(direction * speed * Time.deltaTime);
  27. // if(!isLocalPlayer){return;}
  28. //raycast
  29. Collider2D overlapCol = Physics2D.OverlapCircle(transform.position, castRadius);
  30. if(overlapCol != null){
  31. enemyScript enemy = overlapCol.GetComponent<enemyScript>();
  32. bool hasPlayer = overlapCol.transform.root.GetComponent<playerNetwork>()!=null;
  33. Debug.Log($"{shooterId} shot {overlapCol.name}({netId}), has player? : {hasPlayer}");
  34. if(hasPlayer){return;}
  35. // Debug.Log()
  36. if(enemy == null){
  37. //no enemy
  38. DestroyProjectile();
  39. }else{
  40. OnHit.Invoke(enemy);
  41. DestroyProjectile();
  42. }
  43. }
  44. return;
  45. RaycastHit2D hitRay = Physics2D.CircleCast(transform.position, castRadius, transform.up , distance , isEnemy);
  46. if(hitRay.collider != null){
  47. enemyScript enemy = hitRay.collider.GetComponent<enemyScript>();
  48. bool hasPlayer = hitRay.collider.transform.root.GetComponent<playerNetwork>()!=null;
  49. Debug.Log($"{shooterId} shot {hitRay.collider.name}({netId}), has player? : {hasPlayer}");
  50. if(hasPlayer){return;}
  51. // Debug.Log()
  52. if(enemy == null){
  53. //no enemy
  54. DestroyProjectile();
  55. }else{
  56. OnHit.Invoke(enemy);
  57. DestroyProjectile();
  58. }
  59. // if(hitRay.collider.CompareTag("Enemy")){
  60. // //enemyTakeDamage
  61. // Debug.Log("Enemy Took Magical Damage Range");
  62. // }else{
  63. // DestroyProjectile();
  64. // }
  65. }
  66. }
  67. void DestroyProjectile(){
  68. //Instantiate hit effect
  69. if (isServer)
  70. {
  71. RpcDestroyProjectile();
  72. destroyProjectile();
  73. }
  74. //Destroy(hitEffectVfx, hitLifetime);
  75. }
  76. [ClientRpc]
  77. void RpcDestroyProjectile()
  78. {
  79. destroyProjectile();
  80. }
  81. void destroyProjectile()
  82. {
  83. Instantiate(hitEffectVfx, transform.position, Quaternion.identity);
  84. Destroy(gameObject);
  85. }
  86. void OnDrawGizmos(){
  87. Gizmos.DrawWireSphere(transform.position, castRadius);
  88. }
  89. }