- Home /
Question by
TJeyAGames · Feb 05, 2020 at 09:07 PM ·
raycastraycasthit
Raycast always hitting floor
I am trying to create a first person shooter. The enemy in this game is a robot with a laser gun. The problem is that the raycast which is supposed to detect if the player was hit with the laser never returns a hit for the player and instead hits the floor of the map, no matter where the laser is pointed.
public class EnemyShooting : MonoBehaviour {
public float damage = 10f;
public float flashIntensity = 3f;
public float fadeSpeed = 10f;
public GameObject Player;
public GameObject fx_laserShot_left;
public GameObject fx_laserShot_right;
public Transform laserLeftDirection;
public Transform laserRightDirection;
private Animator anim;
private LineRenderer laserShotLineLeft;
private LineRenderer laserShotLineRight;
private Light laserShotLightRight;
private Light laserShotLightLeft;
private SphereCollider col;
private PlayerHealth PlayerHealth;
public bool shooting;
public bool leftShot;
public bool rightShot;
public float pewRange;
void Awake (){
anim = GetComponent<Animator> ();
laserShotLineLeft = fx_laserShot_left.GetComponent<LineRenderer>();
laserShotLineRight = fx_laserShot_right.GetComponent<LineRenderer>();
laserShotLightRight = fx_laserShot_right.GetComponent<Light>();
laserShotLightLeft = fx_laserShot_left.GetComponent<Light>();
col = GetComponent<SphereCollider> ();
PlayerHealth = Player.gameObject.GetComponent<PlayerHealth> ();
pewRange = col.radius;
}
// Update is called once per frame
void Update () {
float shot = anim.GetFloat(Animator.StringToHash("Shot"));
if (shot > 0.1f && !shooting) {
shooting = true;
if (shot == 1f)
ShootLeft ();
if (shot == 2f)
ShootRight ();
}
if (shot < 1f) {
shooting = false;
}
if (shot != 1f) {
laserShotLineLeft.enabled = false;
leftShot = false;
}
laserShotLightLeft.intensity = Mathf.Lerp (laserShotLightLeft.intensity, 0f, fadeSpeed * Time.deltaTime);
laserShotLightRight.intensity = Mathf.Lerp (laserShotLightLeft.intensity, 0f, fadeSpeed * Time.deltaTime);
if (shot != 2f) {
laserShotLineRight.enabled = false;
rightShot = false;
}
}
public void ShootLeft (){
//Physics.Raycast(origin: fx_laserShot_left.transform.position, direction: fx_laserShot_left.transform.forward, hitInfo: out RaycastHit hit, maxDistance: col.radius);
//Debug.Log("Treffer an Stelle" + hit.transform + "Spieler ist bei" + Player.transform);
if (Physics.Raycast(origin: fx_laserShot_left.transform.position, direction: fx_laserShot_left.transform.forward, hitInfo: out RaycastHit hit, maxDistance: col.radius))
{
if (hit.transform == Player.transform)
{
PlayerHealth.TakeDamage(damage);
Debug.Log("did hit");
}
}
Debug.Log("Hit at " + hit.transform + "Player is at " + Player.transform);
//Effect
laserShotLineLeft.SetPosition(0, laserShotLineLeft.transform.position);
laserShotLineLeft.SetPosition(1, laserLeftDirection.transform.position);
laserShotLineLeft.enabled = true;
laserShotLightLeft.intensity = flashIntensity;
}
public void ShootRight(){
Physics.Raycast(origin: fx_laserShot_right.transform.position, direction: fx_laserShot_right.transform.forward, hitInfo: out RaycastHit hit, maxDistance: col.radius);
if (hit.transform == Player.transform)
{
PlayerHealth.TakeDamage(damage);
}
//Effect
laserShotLineRight.SetPosition(0, laserShotLineRight.transform.position);
laserShotLineRight.SetPosition(1, laserRightDirection.transform.position);
laserShotLineRight.enabled = true;
laserShotLightRight.intensity = flashIntensity;
}
}
Any help would be greatly appreciated.
Comment