90 lines
2.7 KiB
C#
90 lines
2.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class RangeProjectile : NetworkBehaviour
|
|
{
|
|
public float speed;
|
|
public float distance;
|
|
public float lifetime;
|
|
public float hitLifetime;
|
|
public LayerMask isEnemy;
|
|
public Vector3 direction;
|
|
public uint shooterId;
|
|
public float castRadius;
|
|
public GameObject hitEffectVfx;
|
|
|
|
public UnityEvent<enemyScript> OnHit;
|
|
|
|
void Start(){
|
|
// if(!isLocalPlayer){return;}
|
|
Invoke("DestroyProjectile",lifetime);
|
|
}
|
|
|
|
public bool isLocalp;
|
|
void Update(){
|
|
isLocalp = isLocalPlayer;
|
|
transform.Translate(direction * speed * Time.deltaTime);
|
|
// if(!isLocalPlayer){return;}
|
|
//raycast
|
|
|
|
Collider2D overlapCol = Physics2D.OverlapCircle(transform.position, castRadius);
|
|
if(overlapCol != null){
|
|
enemyScript enemy = overlapCol.GetComponent<enemyScript>();
|
|
bool hasPlayer = overlapCol.transform.root.GetComponent<playerNetwork>()!=null;
|
|
Debug.Log($"{shooterId} shot {overlapCol.name}({netId}), has player? : {hasPlayer}");
|
|
if(hasPlayer){return;}
|
|
// Debug.Log()
|
|
if(enemy == null){
|
|
//no enemy
|
|
DestroyProjectile();
|
|
}else{
|
|
OnHit.Invoke(enemy);
|
|
DestroyProjectile();
|
|
|
|
}
|
|
}
|
|
return;
|
|
RaycastHit2D hitRay = Physics2D.CircleCast(transform.position, castRadius, transform.up , distance , isEnemy);
|
|
if(hitRay.collider != null){
|
|
enemyScript enemy = hitRay.collider.GetComponent<enemyScript>();
|
|
bool hasPlayer = hitRay.collider.transform.root.GetComponent<playerNetwork>()!=null;
|
|
Debug.Log($"{shooterId} shot {hitRay.collider.name}({netId}), has player? : {hasPlayer}");
|
|
if(hasPlayer){return;}
|
|
// Debug.Log()
|
|
if(enemy == null){
|
|
//no enemy
|
|
DestroyProjectile();
|
|
}else{
|
|
OnHit.Invoke(enemy);
|
|
DestroyProjectile();
|
|
|
|
}
|
|
// if(hitRay.collider.CompareTag("Enemy")){
|
|
// //enemyTakeDamage
|
|
// Debug.Log("Enemy Took Magical Damage Range");
|
|
// }else{
|
|
// DestroyProjectile();
|
|
// }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void DestroyProjectile(){
|
|
//Instantiate hit effect
|
|
Instantiate(hitEffectVfx , transform.position , Quaternion.identity);
|
|
Destroy(gameObject);
|
|
//Destroy(hitEffectVfx, hitLifetime);
|
|
}
|
|
|
|
void OnDrawGizmos(){
|
|
Gizmos.DrawWireSphere(transform.position, castRadius);
|
|
}
|
|
|
|
}
|