- Home /
Question by
kylerpc06 · Jun 10, 2019 at 03:02 PM ·
raycastraycastingraycasthit
issues with raycasts
I posted this on reddit but didn't get the answers i wanted.
so, i have a script where i shoot with a raycast, and it deletes certain entities. issue is, if i shoot something from the top of the collider, it doesn't register a hit. anyone know why?
here are the scripts:
raycast:
Raycast:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycast : MonoBehaviour
{
public Collider enemy;
public float damage;
public float range;
public Transform FpsCam;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(FpsCam.transform.position, FpsCam.transform.forward, out hit, range))
{
Raycastdeath enemy = hit.transform.GetComponent<Raycastdeath>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
}
enemy being hit by raycast:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycastdeath : MonoBehaviour
{
public float health;
public void TakeDamage (float amount)
{
health -= amount;
if (health <= 0f)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
}
}
Comment
Have you tried printing out the name of the object you're hitting to see if it is actually colliding with the right one?
"shoot something from the top of the collider" please clarify