Unity2d Move at an angle with out rotating box collider

Hello!

I’ve been trying to make a 2d platformer. I’ve been following this tutorial

Which is very good because I like the way it handles collisions. I want to add an 8 way dash like in Marvel vs Capcom so you can move in different angles in the air.

I used this to get the dash

Which works fine for dashing straight ahead but when i try to get to dash in a 45 or 90 degree angle it does some weird stuff. I’m thinking its because I’m rotating my box collider which screws up the ray casting collisions or something.

Well is there any way I could move in a specific angle with out rotating my collider?

Here is my Dash coroutine

IEnumerator Boost(float boostDur, Vector2 boostVelocity, Quaternion boostAngle) //Coroutine with a single input of a float called boostDur, which we can feed a number when calling
    {
   
        isBoosting = true;
        float time =0; //create float to store the time this coroutine is operating
        canBoost = false; //set canBoost to false so that we can't keep boosting while boosting
   

        while(boostDur > time) //we call this loop every frame while our custom boostDuration is a higher value than the "time" variable in this coroutine
        {
            time += Time.deltaTime; //Increase our "time" variable by the amount of time that it has been since the last update

            transform.rotation = boostAngle;
            //transform.rotation = Quaternion.Euler( 0, 0, 45 );

            GetComponent<Rigidbody2D>().velocity = boostVelocity*1.2f; //set our rigidbody velocity to a custom velocity every frame, so that we get a steady boost direction like in Megaman
            yield return 0; //go to next frame
        }
   

        isBoosting = false;
        anim.SetBool ("Boosting", false);
        yield return new WaitForSeconds(boostCooldown); //Cooldown time for being able to boost again, if you'd like.



        canBoost = true; //set back to true so that we can boost again.

    }

You pretty much never want to rotate on any axis except Z.

You can move in a direction by multiplying a direction (normalized vector) by the distance to move.

So to move 50 units to the right it would be: Vector3.right * 50f.

This would move your character at a 45 degree angle by 10 units.

float angle = 45;
float distance = 10;

// create a rotation 45 degrees around the Z axis
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Vector3 direction = Vector3.right;  // normalized vector

// apply the rotation to the direction
Vector3 rotatedDirection = rotation * direction;

// move the transform "distance" units along the direction
transform.Translate(rotatedDirection* distance);

Thanks for replying, Your method works but it doesn’t account for collisions,

I used a different method that changes my x and y velocities

velocity.x = Mathf.Cos(angle) * speed;
velocity.y = Mathf.Sin(angle) * speed;

which seems to be working well and I don’t go clipping through my walls.

If you need this to power a physics movement, then you can use “rigidbody.MovePosition” instead of “transform.Translate”.

or for a non-instant movement you can use “rigidbody.AddForce”, and mutiply distance by delta time each frame to make it distance per second, or speed.

1 Like