Search Unity

Unity2d Move at an angle with out rotating box collider

Discussion in '2D' started by akindhobo, May 17, 2016.

  1. akindhobo

    akindhobo

    Joined:
    Mar 28, 2016
    Posts:
    2
    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

    http://forum.unity3d.com/threads/2d-platformer-mega-man-x-style-dash.228648/

    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

    Code (CSharp):
    1. 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
    2.     {
    3.    
    4.         isBoosting = true;
    5.         float time =0; //create float to store the time this coroutine is operating
    6.         canBoost = false; //set canBoost to false so that we can't keep boosting while boosting
    7.    
    8.  
    9.         while(boostDur > time) //we call this loop every frame while our custom boostDuration is a higher value than the "time" variable in this coroutine
    10.         {
    11.             time += Time.deltaTime; //Increase our "time" variable by the amount of time that it has been since the last update
    12.  
    13.             transform.rotation = boostAngle;
    14.             //transform.rotation = Quaternion.Euler( 0, 0, 45 );
    15.  
    16.             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
    17.             yield return 0; //go to next frame
    18.         }
    19.    
    20.  
    21.         isBoosting = false;
    22.         anim.SetBool ("Boosting", false);
    23.         yield return new WaitForSeconds(boostCooldown); //Cooldown time for being able to boost again, if you'd like.
    24.  
    25.  
    26.  
    27.         canBoost = true; //set back to true so that we can boost again.
    28.  
    29.     }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    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.
    Code (CSharp):
    1. float angle = 45;
    2. float distance = 10;
    3.  
    4. // create a rotation 45 degrees around the Z axis
    5. Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    6.  
    7. Vector3 direction = Vector3.right;  // normalized vector
    8.  
    9. // apply the rotation to the direction
    10. Vector3 rotatedDirection = rotation * direction;
    11.  
    12. // move the transform "distance" units along the direction
    13. transform.Translate(rotatedDirection* distance);
     
    Last edited: May 17, 2016
  3. akindhobo

    akindhobo

    Joined:
    Mar 28, 2016
    Posts:
    2
    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.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    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.
     
    theANMATOR2b likes this.