Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Trick-or-Treat: Collision Code for Unity

Greetings, fellow Unity degenerates and coding wizards! 🧙‍♂️✨

Today, we're delving into the arcane world of collision detection. In the spirit of Halloween, I've prepared a special treat for you, a very special version of the collision code I commonly use with Unity for retro-style games. But be wary, for there lies a trick—a bug I've cunningly concealed within the incantations of code for you to uncover and vanquish. Watch the video above for a full breakdown and the secret to the fix!


//Get our box collider information
    public BoxCollider collider;
    public LayerMask collisionMask;
    void Move(Vector2 delta)
        {
        // Set up some basic variables
        Vector3 halfSize = collider.bounds.size/2f;
        Vector2 shortestLength = new Vector2(Mathf.Infinity, Mathf.Infinity);
        Vector2 hitPoint = new Vector2(0f, 0f);
        float skin = .005f; 
        //Store the position since transform changes don't update until the end of the frame
        Vector3 position = transform.position;
        //If we are moving left or right (-1 or 1)
        var xDir = Mathf.Sign(delta.x);
        var collidedX = false;
        if (xDir != 0)
            {
            var rayDistance = Mathf.Abs(delta.x) + halfSize.x + skin;
            var rayDirection = Vector3.right * xDir;
            for (var yy = 0; yy < 3; yy ++)
                {
                //Calculate the start position of our raycasts
                Vector3 rayPosition;
                rayPosition.x = position.x;
                rayPosition.y = position.y + halfSize.y - halfSize.y * yy;
                rayPosition.z = position.z;
                
                Debug.DrawRay(rayPosition, rayDirection, Color.yellow);
                RaycastHit hit;
                //Does the ray intersect with any object in our defined layermask
                if (Physics.Raycast(rayPosition, rayDirection, out hit, rayDistance, collisionMask))
                    {
                    //Get the distance between the collision and the edge of our collider
                    var hitLength = hit.point.x - (rayPosition.x + halfSize.x * xDir);
                    //Get the smallest point of collision
                    if (hitLength < shortestLength.x)
                        {
                        shortestLength.x = hitLength;
                        collidedX = true;
                        hitPoint.x = hit.point.x;
                        }
                    }
                }
            if (collidedX)
                {
                //Move our collider to the hit point
                position.x = hitPoint.x - (halfSize.x + skin) * xDir;
                delta.x = 0;
                }
            }
        //If we are moving up or down (-1 or 1)
        var yDir = Mathf.Sign(delta.y);
        var collidedY = false;
        if (yDir != 0)
            {
            var rayDistance = Mathf.Abs(delta.y) + halfSize.y + skin;
            var rayDirection = Vector3.up * yDir;
            for (var xx = 0; xx < 3; xx ++)
                {
                //Calculate the start position of our raycasts
                Vector3 rayPosition;
                rayPosition.x = position.x + halfSize.x - halfSize.x * xx;
                rayPosition.y = position.y;
                rayPosition.z = position.z;
                
                Debug.DrawRay(rayPosition, rayDirection, Color.yellow);
                RaycastHit hit;
                //Does the ray intersect with any object in our defined layermask
                if (Physics.Raycast(rayPosition, rayDirection, out hit, rayDistance, collisionMask))
                    {
                    //Get the distance between the collision and the edge of our collider
                    var hitLength = hit.point.y - (rayPosition.y + halfSize.y * yDir);
                    //Get the smallest point of collision
                    if (hitLength < shortestLength.y)
                        {
                        shortestLength.y = hitLength;
                        collidedY = true;
                        hitPoint.y = hit.point.y;
                        }
                    }
                }
            if (collidedY)
                {
                //Move our collider to the hit point
                position.y = hitPoint.y - (halfSize.y + skin) * yDir;
                delta.y = 0;
                }
            }
        //Apply our translation
        position.x += delta.x;
        position.y += delta.y;
        transform.position = position;
        }

Until our paths cross again in the ethereal plane of game development, Happy Coding and Happy Halloween! 🎃

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.