Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question fast objects pass through other objects?

Discussion in 'Physics' started by tsmspace, Aug 30, 2023.

  1. tsmspace


    Oct 6, 2019
    I realize this must be how tricks like ender-pearling to get above the nether work.

    If an object is moving fast, then it occupies only that space that frames calculate it to be in,, so once it gets enough speed, it can skip through colliders.

    this MUST be a topic of somewhat regular discussion?? I am not a programmer, but I am trying to make a racing game,, and basically I'm struggling to register collisions with checkpoints. its' all working great but I know players will want to crank the settings to speed up the velocity they travel,, and at a point the checkpoints just don't work anymore,, they think the player is going a different direction, or they don't know they've been hit at all.

    I don't think I'm alone with this struggle,, (for example the ender pearls are warping through the blocks, ) but what is there to know?? IS this something that just needs faster computers ??
  2. spiney199


    Feb 11, 2021
  3. Murgilod


    Nov 12, 2013
    Change your collision mode to either Continuous Dynamic or Continuous Speculative. By default, Unity uses discrete detection for rigidbodies, which is very fast but also suffers from extreme accuracy issues at high (and even not so high) speeds.
    spiney199 likes this.
  4. tsmspace


    Oct 6, 2019
    I think I noticed a change but it's still got the problem. continuous speculative seems to be the best. At the speeds I play at,, there's generally not a problem,, but I put it so that players can set their power,, and up at the higher speeds it's basically a 100% failure rate.
  5. Ryiah


    Oct 11, 2012
    One way of handling this is to keep track of the position of the player each frame, and cast a ray from the current position to the previous position. If the ray hits the checkpoint then you know they passed through it. Here's some example code generated with ChatGPT that shows how to cast from one Vector3 to another Vector3.

    Code (csharp):
    1. using UnityEngine;
    3. public class RaycastExample : MonoBehaviour
    4. {
    5.     public Vector3 startPoint;
    6.     public Vector3 endPoint;
    8.     void Update()
    9.     {
    10.         // Calculate the direction from the start point to the end point
    11.         Vector3 direction = endPoint - startPoint;
    13.         // Normalize the direction
    14.         direction.Normalize();
    16.         // Ray length
    17.         float rayLength = Vector3.Distance(startPoint, endPoint);
    19.         // Perform the raycast
    20.         RaycastHit hitInfo;
    21.         if (Physics.Raycast(startPoint, direction, out hitInfo, rayLength))
    22.         {
    23.             // The ray hit something
    24.             Debug.Log("Hit " +;
    25.         }
    26.         else
    27.         {
    28.             // The ray did not hit anything
    29.             Debug.Log("No hit");
    30.         }
    32.         // Draw the ray in the scene view for debugging
    33.         Debug.DrawRay(startPoint, direction * rayLength,;
    34.     }
    35. }
  6. tsmspace


    Oct 6, 2019

    I really want to investigate this,, actually,, since I'm such a terrible programmer although I do think I would eventually figure it out. It looks simple enough to get started.. Just now I went with making the collider on my character that will trigger the racing gates very long (like a long tail).

    I honestly am concerned that the raycast will still miss gates, and I'm not 100% sure how to go about handling the direction check. First of all,, I feel like the collider would still need to be beefy on the gate,, but I saw somewhere that collisions are only on the skin of the mesh.

    anyway my very long tail managed to register all of my test passes through the gate in the editor, while NOT playing the game (just going in a straight line through some) ,,, I think that there will be issues, though, with it triggering things I don't want it to.
  7. Ryiah


    Oct 11, 2012
    It's not possible for a ray to miss a collider in the same way that your colliders are missing each other. When you cast a ray it doesn't actually move. Instead the engine uses intersection math (linked below) to determine if it will hit any colliders in its general direction.

    If you're concerned that the infinitely small point that is a ray may miss the collider (eg maybe the player just barely passed through a checkpoint but the ray is cast from the middle of the player missing the gate) you can use one of these instead. They work the same as raycasts but let you define a shape.
    Last edited: Aug 31, 2023
    impheris likes this.