- Home /
Raycasting is hitting objects and not hitting them at the same time
My level is a small closed room with colliders where a raycast should not be able to miss. I was getting null reference exceptions so I put the raycast in an if statement, which took away the error but both this line Debug.LogError("Raycast hit nothing");
and this line Debug.Log(hit.transform.gameObject.tag);
were being called at the same time constantly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class playerShooting : MonoBehaviour
{
public Transform cam;
//public GameObject greyCross;
public GameObject redCross;
int layerMask = 1 << 8;
string scene = "";
float range;
bool isAimingAtEnemy = false;
void Start()
{
scene = SceneManager.GetActiveScene().name;
range = Mathf.Infinity;
}
void Update()
{
layerMask = ~layerMask;
/*if (scene == "SampleScene")
{
if (Input.GetButtonDown("Fire1"))
{
RaycastHit hit;
Physics.Raycast(cam.position, cam.transform.forward, out hit, 100f);
if (hit.transform == null) return;
if (hit.transform.gameObject.tag == "tower")
{
hit.transform.Translate(-Vector3.up*0.5f);
}
}
if (Input.GetButtonDown("Fire2"))
{
RaycastHit hit;
Physics.Raycast(cam.position, cam.transform.forward, out hit, 100f);
if (hit.transform == null) return;
if (hit.transform.gameObject.tag == "tower")
{
hit.transform.Translate(Vector3.up*0.5f);
}
}
}*/
else if (scene == "mini map" || scene == "shooting game")
{
CheckIfAimingAtObject();
if (isAimingAtEnemy) redCross.SetActive(true);
else redCross.SetActive(false);
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
}
private void Shoot()
{
}
private void CheckIfAimingAtObject()
{
RaycastHit hit;
if (Physics.Raycast(cam.position, cam.TransformDirection(Vector3.forward), out hit, range, layerMask))
{
Debug.Log(hit.transform.gameObject.tag);
if (hit.transform.gameObject.tag == "enemy") isAimingAtEnemy = true;
else isAimingAtEnemy = false;
}
else
{
Debug.LogError("Raycast hit nothing");
}
}
}
I'm using unity 2019.3.9. It doesn't seem to be causing any problems but it would be nice to know why this is. Thanks!
Is it possible that you have a copy of this script on some other object unintentionally? This seems like the most likely issue to me.
Double checked, only one object has the script. It does return both while I'm not moving. Now, I'm having a similar, separate problem. The enemy in my game uses a raycast to check if the player is visible (no walls blocking) before the enemy starts shooting. I used this line: Debug.DrawLine(transform.position, hit.point, Color.green, 20f);
to check where the raycast is hitting and the ray is going straight through the player. Now, it's not hitting at all until i bump into the enemy a few times.
Answer by $$anonymous$$ · Mar 26, 2021 at 10:22 PM
Do not over complicate the raycast method to excited it you just need this
Void update ()
{
If (// your input method )
{
RaycastHit hit;
If( physics.raycast ( //start position, // direction for example Cam.Forward, out hit, // range or anything that you want optional)
{
// anything you want to do.
}
}
}
Thanks, I will in the future. This doesn't seem to be causing any more issues, thanks.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Raycast only works while moving 2 Answers
How can I create a 3d mesh using raycasts? 0 Answers
Scale On One Side Only 0 Answers