简体   繁体   中英

Why does my raycast sometimes does detect a hit and sometimes wont?

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?

编辑器截图

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 :/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM