- Home /
Why do my bullets only destroy themselves sometimes after colliding with the wall?
if (Physics.Raycast(myTransform.position, myTransform.up, out hit, range) &&
expended == false)
{
//if the collider has the tag of Floor then...
if (hit.transform.tag == "Wall")
{
expended = true;
//make the projectile become invisible
myTransform.renderer.enabled = false;
//turn off the light component
myTransform.light.enabled = false;
}
Pretty much half the time the code works and the bullet gets deleted, but the other half of the time the bullet goes right through the wall. I have no idea why this is happening and would like any input that might solve the problem, thanks.
Are you sure the bullet actually touches the wall ? If not, check your wall's collider
Need to see more context. Under what circumstances is this code executed? Why 'myTransform.up'...is this the direction the bullets are fired and if so, is it still the direction the bullets are moving when they hit the wall? You talk about colliding, are you using Physics and if so are you getting the OnColliderEnter() event? Is it possible the Raycast() is hitting something other than a 'Wall' just before they go through?
It's not touching anything before the wall and I'm not using any oncolliderenter just if the ray cast comig from the bullet hits something it will delete itself. (The bullet)
I see two possibilities. The first is that the bullet is moving too fast for the 'range' parameter. That is in one frame it is too far away, and in the next frame it is beyond the surface of the wall. You can test this by increasing the range or by slowing the bullet down. The other possibility is that myTransform.up is not facing the wall when the bullet goes through. You can test this by adding
Debug.DrawRay(myTransform.position, myTransform.up * range);
I can think of several ways to fix either problem.
If the issue is the range/speed, then why is happens will depend on whether you are moving your bullets through the Transform or are moving them Physics. But either way it is a ti$$anonymous$$g issue.
If you are moving through the transform you are likely using Time.deltaTime. This value varies from frame to frame, so how much the bullet moves each frame varies. If you were to record all the positions of the bullet as it flies towards the wall, you would see that no two bullets have the same set of positions. In that case, some bullets are placed so that they detect the wall, and some bullets will skip go from not detecting the wall one frame and past the wall the next.
For Physics movements, you are using a fixed timestep, but that timestep does not align with Update() the same way for each bullet. So the ti$$anonymous$$g of your check with respect to the bullet position will vary with each bullet.
Your answer
Follow this Question
Related Questions
Raycast from inside an object 2 Answers
Raycast shooting problems 1 Answer
Ammo Pickup 0 Answers
Object has many cube colliders in different angles and collides with more than one object. 0 Answers
Make raycast ignore some triggers but hit some others? 2 Answers