- Home /
C# Raycast is only hitting one item and then stopping
I have a raycast to detect when my character's gun is pointed at a rabbit, but it detects if it's pointed at an animal in general, so I have another if statement check to see if the animal it's pointed at is a rabbit. I have the debug log printing a statement when each of these things happens. it should print every time i point at anything, and then a different statement every time it points at a rabbit. however, it only prints each statement once, and all the functions that are supposed to happen to the rabbits only happen to the first rabbit it sees, which doesn't even seem to be the first rabbit i point at.
void bunnycheck()
{
Physics.Raycast(gunPoint, out gunHit, 500f, bunnyMask);
}
void Update()
{
InvokeRepeating("bunnycheck", 1, 1);
Debug.DrawRay(gunPoint.origin, gunPoint.direction, Color.red, 50f, false);
if (enemyname.Equals("Bear"))
{
//bear stuff, whatever
}
else if (enemyname.Equals("Bunny"))
{
if (Physics.Raycast(gunPoint, bunnyMask))
{
Debug.Log("something was hit");
//this only ever prints once
if (transform.gameObject.GetComponent<Collider>() == gunHit.collider)
{
Debug.Log("bunny hit");
//this also only ever prints once, sometimes on a different object than the first print. and the following code only ever happens to one bunny
nav.enabled = false;
transform.Translate(new Vector3(1, 0, 0) * Time.deltaTime);
nav.enabled = true;
}
}
I'm at a loss here. It's very possible that the issue isn't even with the code i've posted here, but if you need some other part I can post it, or even just a confirmation that this code is fine I guess would help. The DrawRay seems to draw a bunch of very short rays, which is also baffling. I don't know why they're so short - I specifically set the length to 500 units so it definitely reaches all the way across the terrain, but they're always still short anyway.
first of all i dont think invoke should be in update try start ins$$anonymous$$d and your drawray is 50 not 500
Answer by Remy_Unity · Apr 24, 2018 at 12:23 PM
I think using InvokeRepeating in the update loop is the worst idea here.
Basicaly, you're asking at every frame, to invoke "bunnycheck" after 1 second, every seconds. So, after some time (probably a few seconds), you're probably going to fire multiple rays per frame, each one overriding the previous result.
Like @Reedex said : you probably want to call InvokeRepeating at start, or simply call "bunnycheck()" at the beginning of Update.
Secondly, I don't see how you set the value of enemyname. is it gunHit.collider.gameObject.name ?
How even can this line compile : if (Physics.Raycast(gunPoint, bunnyMask)) ? Raycast( Vector3, LayerMask) isn't a valid call ??? If you replace InvokeRepeating of update by the raycast done in bunnycheck, you can get if the ray has hit anything, and what was hit, an this will probably be simpler.
enemyname is something declared elsewhere, because the enemies are clones of each other so they don't all have the same name - some of them are bunny and some are bunny(clone). I've checked it, though, and it dubs each enemy with the correct name, using some stuff with the spawn point.
gunPoint is a ray with the origin at the tip of the gun and the direction shooting forward. I didn't think to include it's declaration here because it's, like, fifty or a hundred lines up, haha. It was originally if(Physics.Raycast(gunPoint, out gunHit, 500f, bunny$$anonymous$$ask)), but it did the same thing, so I was worried it only shot the once and then never again, which. doesn't make sense. but I've been trying everything I can think of on this.
I've moved bunnycheck to the beginning of update, without any invoke calls or anything, which makes a lot more sense, but it still has the same issue. What strikes me as odd is that it does successfully print "bunny hit" once, but only once - so it can clearly get there, but after the one time it just doesn't any more?
This test seems suspicious :
if (transform.gameObject.GetComponent() == gunHit.collider)
You are testing is the collider attached to the same gameObject as the script is the one hit byt the ray. Is this what you're expecting to do ?
An then you translate the object where the script is, so obviously, also the collider.
Possibly, this is what is happening : at the first frame, the ray is hitting the collider that is attached to the same GO as the script, and then moved a bit to the right. Then because it has moved, it's no more hit by the ray, and doesn't trigger the debug message anymore.
Also, dummy question, can you check if your don't have "Collapse" enabled in the console, that would make you think that the message is send only once ?
Each bunny has its own copy of the script, so it needs to make sure the one being hit by the ray is the one it's moving before it goes moving it.
Actually, you know, this is stupid, but forgetting that I had collapse enabled is exactly what I did here. It still for some reason doesn't always catch the first few bunnies, but the majority of them are actually sending the message and I didn't see...!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
c# Raycast going off at odd angles. Unity 5 2 Answers
WorldToScreenPoint returning ridiculously high number 1 Answer
OnMouseButtonDown raycast 2 Answers