- Home /
Question by
SmileJakoby · Apr 16, 2020 at 04:57 PM ·
c#fps
FPS grenade line of sight check
So, I'm making an FPS right now and I've coded in a functioning grenade. It works exactly the way it should, but I want to know how to make a LOS check so that the explosion damage doesn't go through walls.
public class GrenadeDamage : MonoBehaviour
{
public Rigidbody Rigid;
public GameObject explosionEffect;
public float radius;
public int Lifetimer;
public int DamageAmount;
void Start()
{
//the initial velocity of the grenade
Rigid.AddForce(new Vector3(0, 20, 200));
}
void FixedUpdate ()
{
Lifetimer -= 1;
if (Lifetimer == 0)
{
Explode();
}
}
void Explode()
{
Instantiate(explosionEffect, transform.position, transform.rotation);
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach (Collider col in colliders)
{
EnemyAI enemy = col.GetComponent<EnemyAI>();
if (enemy != null)
{
// linear falloff of effect
float proximity = (transform.position - enemy.transform.position).magnitude;
float effect = 1 - (proximity / radius);
// damage cap so that the damage isn't extreme at point blank (for easier balancing)
if (effect > 0.7f)
{
effect = 0.7f;
}
// deal damage to enemy
enemy.DeductPoints(DamageAmount * effect);
}
}
Object.Destroy(this.gameObject);
}
}
Comment
Answer by SmileJakoby · Apr 16, 2020 at 05:46 PM
I did it! I figured out how to do it. I replaced enemy.deductpoints with RaycastHit hit; Vector3 direction = enemy.transform.position - transform.position; if (Physics.Raycast(transform.position, direction.normalized, out hit)) { hit.transform.SendMessage("DeductPoints", effect * DamageAmount, SendMessageOptions.DontRequireReceiver); }