Dash Collision

Hi, I am making a game where the main mechanic is dashing through enemies. However, with regular/default collision systems it cannot detect the collision because dash is really fast that it looks like it is teleporting. The following is a sample of the dash script where pressing Shift causes the player to dash:

        if (Input.GetKey(KeyCode.LeftArrow)) {
            //regular movement 
            transform.Translate(Vector3.left * speed * Time.deltaTime);

            //dashing
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                transform.Translate(Vector3.left * speed * slash_speed * Time.deltaTime);
            }
        }

is there any way to detect the collision? I was thinking of a ray cast, but I feel like Unity’s collision should work.

Hi, you’re right Unity’s collision should work, and it does for the most part.

Transform.Translate is not one of those times though, and it’s like that by design. What translate does is pretty much teleport your object around, without accounting for physics, collision or anything like that.


If you’re working on a character controller I would highly suggest you stop using Translate. You can slap a rigidbody on your character and start using that. You can use AddForce, modify your rigidbody’s velocity, use MovePosition, etc. Anything but Translate for character control.

After doing some more research and experimentation, I was able to get the following that works just fine!

        if (Input.GetKey(KeyCode.LeftArrow)) {
            //regular movement 
            transform.Translate(Vector3.left * speed * Time.deltaTime);

            //dashing
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                RaycastHit slash;
                Vector3 pre_pos = transform.position;
                transform.Translate(Vector3.left * speed * slash_speed * Time.deltaTime);
                if (Physics.Linecast(pre_pos, transform.position, out slash)) {
                    Debug.Log("HIT " + slash.collider.name);
                    Debug.DrawRay(pre_pos, transform.position, Color.red, 5f);
                    if (slash.collider.tag == "Enemy") {
                        Destroy(slash.collider.gameObject);
                    }
                }
            }
        }

NOTE: From what I have seen it can only save the information from the first thing it collides with!