0
votes

I have a bullet that is fired with a Impuls on the rigidbody. Then, each frame i do a raycast forward with the speed of the bullet, which sometimes does, and sometimes doesn't find an enemy. When i skip through the game frame by frame, i can clearly see my debug-raycast inside the (boc)collider of the enemy, but it still won't find it. Im pretty sure it doesn't just pass the enemy, in the editor i can clearly see the green raycast inside the collider.

Any suggestions? I also tried with a linecast, which i found on here, but that gives the same result. (the line is on the exact same position and also draws the debug-line inside the collider. The layers are also correct, as i said, sometimes it will, and sometimes it wont find the target...

void Update()
{
  //get direction and distance
  Vector3 direction = _rigidBody.velocity.normalized;
  float distance = (_rigidBody.velocity.magnitude * Time.deltaTime);

  //raycast for targets
  RaycastHit raycast;
  if (Physics.Raycast(transform.position, direction, out raycast, distance, HitLayerMask))
  {
    Debug.DrawRay(transform.position, direction, Color.red);
  } 
  else
  {
    Debug.DrawRay(transform.position, direction, Color.green);
  }
}

You can see the tiny debug ray, aswell als the collider from side-view. It also it almost right in the middle from the front-view. I'm thinking it might be because the start and end of the raycast are inside the boxcollider?

Editor Screenshot

the rigidbody is moved by

//Speed = 500 in this case, but lowering doesn't change anything
rigidBody.AddRelativeForce(Vector3.forward * Speed, ForceMode.Impulse);

UPDATE:

After some more debugging and testing, i found that it happens because you won't get a hit from inside a collider. Here's what happend:

  • bullet is outside target collider, but raycast is to short to find it.

  • bullet moves forward, enemy also moves forward (closing in on eachother). Bullet is now inside the collider

  • raycast won't find enemy because thats just how raycast work

What i could do, is make the distance of the raycast further, but also not so far away that it would result in weird hits that would graphically look weird.

i thought that using

float distance = (_rigidBody.velocity.magnitude * Time.deltaTime);

would be enough (since thats the distance the bullet traveled since last frame, but because the enemy also moves, the above can happen. Well, it would be enough if the enemy wouldn't move :/

1
Maybe the distance(deltaTime) is too much, you can check if it's larger than your object and split it and test each step in a for loop. btw, I know NOTHING about graphics, just some thought, don't quote me on this.Behrooz
I'll try to set the distance manually and see if that works, but i dont want to cast to far ahead, since that could result in weird behaviour (hitting before it actually hits)Marijn
no, you're doing the opposite of what i said, you don't need to do x * distance, you need to do (x * distance/m), 1 <= x <= mBehrooz
Best is to use a linecast from previous to current position. Or continuous detection on rigidbody.Everts
its the same thing with a linecast, its also right inside the boxcollider, still doesnt detect it, image is pretty much the same as above with raycastMarijn

1 Answers

0
votes

One possible solution is to go to Project Settings -> Script execution order and put the bullet's MonoBehavior after Default Time. That way everything else has already finished moving before you do the ray cast. A hacky solution but should work.