fast objects pass through other objects?

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 ??

You probably want to read this: Unity - Manual: Continuous collision detection (CCD)

1 Like

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.

1 Like

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.

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.

GPT-4

using UnityEngine;

public class RaycastExample : MonoBehaviour
{
    public Vector3 startPoint;
    public Vector3 endPoint;

    void Update()
    {
        // Calculate the direction from the start point to the end point
        Vector3 direction = endPoint - startPoint;

        // Normalize the direction
        direction.Normalize();

        // Ray length
        float rayLength = Vector3.Distance(startPoint, endPoint);

        // Perform the raycast
        RaycastHit hitInfo;
        if (Physics.Raycast(startPoint, direction, out hitInfo, rayLength))
        {
            // The ray hit something
            Debug.Log("Hit " + hitInfo.collider.name);
        }
        else
        {
            // The ray did not hit anything
            Debug.Log("No hit");
        }

        // Draw the ray in the scene view for debugging
        Debug.DrawRay(startPoint, direction * rayLength, Color.red);
    }
}

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.

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.

https://en.wikipedia.org/wiki/Intersection_(geometry)

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.

https://docs.unity3d.com/ScriptReference/Physics.BoxCast.html

1 Like