- Home /
Raycast won't fire
My raycast won't fire. That's literally it.
I tried checking with a Debug.DrawRay but it just doesn't show.
Debug.DrawRay(transform.position, fwd2, Color.green);
It's been like this for about a month now and I've tried everything.
I have multiple instances where my raycasts wont work, but they're all set up roughly the same. Here's one instance:
Vector3 fwd = player.transform.TransformDirection(Vector3.forward);
Debug.Log("fwd Defined");
RaycastHit hit;
Debug.Log("RaycastHit hit was made");
if (Physics.Raycast(transform.position, fwd, out hit))
{
Debug.Log("Raycast Fired And Hit Something");
string tagPlaceholder = hit.collider.gameObject.tag;
Debug.Log("Tag Found");
if (tagPlaceholder == "Enemy")
{
Debug.Log("Is Enemy");
hit.collider.gameObject.GetComponent<Enemy>().onFire = true;
}
The farthest the Debug.Log's get is to RaycastHit hit was made. I had a piece of code that checked if it fired at all, and it would always return false. I don't know what happened to that piece of code (I probably got rid of it) and I can't find it again.
How are we supposed to help you debugging your code if you don't provide it?
Try passing to the Physics.Raycast method as last parameter "$$anonymous$$athf.Infinity" for distance. If still nothing, declare a public Layer$$anonymous$$ask variable, set it to everything in the inspector and pass it as parameter after $$anonymous$$athf.Infinity. If still nothing, we'll try something else
Also, I noticed that you used the player.transform for the TransformDirection, but transform for the raycast. I can't be sure, but don't you need the same transform for both?
Yeah, I changed that, the Debug.DrawRay() was being sent out of some empty object and it still worked after I changed it but stopped working about a $$anonymous$$ute afterwards.
Sorry, made a dumb mistake the Debug.DrawRay() still works but the raycast itself still doesn't.
Dumb questions, but better be safe:
When is this code called? Are you even sure it is called?
Are you sure you haven't disabled the info logs in your console
Are you sure you have colliders in your scene?
Are you sure your colliders are not in the
IgnoreRaycast
layer?Do you see anything in your scene if you put a
Debug.DrawRay
before callingPhysics.Raycast
? (make sure your gizmos aren't disabled)
Its called when I click spacebar and the Debug.logs tell me its called.
They're enabled.
Yep, have colliders.
They're in the default layer.
I tried it again and Debug.DrawRay() does make a line but like... my raycast doesn't work...
Are you sure your colliders are not set to trigger? If so, you have to specify the queryTriggerInteraction
parameter.
Answer by tadadosi · Jun 05, 2020 at 12:53 AM
I grabbed your code, added a Debug.DrawRay on top of Physics.Raycast, changed the tag condition inside the raycast check, created 1 box to add this script, another box to add the tag Enemy to it, aligned this box to the ray direction and it works just fine. Don't know exactly what you are missing, it should be pretty straight forward. (Maybe the player.transform part?)
void Update()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Debug.Log("fwd Defined");
RaycastHit hit;
Debug.Log("RaycastHit hit was made");
Debug.DrawRay(transform.position, fwd * 3f, Color.red);
if (Physics.Raycast(transform.position, fwd, out hit))
{
if (hit.collider.CompareTag("Enemy"))
{
Debug.Log("Enemy Found!");
}
}
}
$$anonymous$$aybe, but thats the issue im having. One day it was working just fine and the next it just doesnt. Idk.
Have you tried creating a new project and adding your code and the scene setup to see if it works?
So I tried making a new scene and just copy and pasting the code and that worked.
Then, I tried copy and pasting all of the files from the project to a new one. That didn't work.
Then, I tried to call the raycast from a different script and that worked. So, I guess I'll just do that.
Idk, maybe that script is corrupted or something but eh. Found another way around.
Thanks everybody for help.
Answer by sheepmccree · Jun 04, 2020 at 06:43 PM
Debug.DrawRay()'s 'direction' parameter has to have implicitly the length of the ray. So multiply that direction vector by the desired length for your ray. For example:
Debug.DrawRay(transform.position, fwd2 * 5, Color.green);
Would give you a ray 5 units long. (Assuming that 'fwd2' is a direction vector)
I tried doing this.
Debug.DrawRay(transform.position, fwd2 * 10, Color.green);
But it still doesn't show anything. The raycast just doesn't fire and I don't know why.
Then a longer code snippet or context would be useful, there aren't any other things to go on from
That depends where you are putting the Debug.DrawRay, because you are not definding 4th parameter what is duration, so if you are doing Debug.DrawRay not in some kind of Update functions which has consistent execution then your ray will be drawn only for 1 frame and then gone, therefore you won't notice it.
Debug.DrawRay(transform.position, fwd2 * 10, Color.green, 50f);
For example, the code above will create ray that exists for 50 seconds.
It was in the update method. Probably should have put that... woops...
Answer by Eno-Khaon · Jun 05, 2020 at 01:22 AM
Is(are) your script(s) turned on for displaying Gizmos in your scene? Also, all gizmos can be toggled as a whole in the Game view.
It wouldn't necessarily solve a problem of a normal Raycast, but it would hopefully serve to improve troubleshooting capability.
A problem I've encountered in the past is that Raycasts don't like to reliably make proper contact at incredibly small or large scale (like, a 0.0001 or 100000 radius sphere as a target). This wouldn't explain why you've been unable to see proper visualizations on Debug.DrawRay(), though.
It's hard to say why it would stop functioning as expected, let alone why you would seem to lose the Debug line drawing functionality. The only other thought that comes to $$anonymous$$d so far @tadadosi already covered (if there's any disconnect between player.transform and simply transform, but I'm guessing it's something like a script on a weapon, judging by the onFire Boolean you adjust)
Your answer
Follow this Question
Related Questions
OnCollisionEnter2D not being called on entering new tile once already called. 1 Answer
c# How do I set up continuous raycast collision detection? 1 Answer
Raycast Mouse Click On Specific Objects Only 0 Answers
Is it possible to check how much a trigger is filled by %? 1 Answer
Creating 2d image map of objects based off 3d space colliders Unity C# 0 Answers