Question by
imfinethanks · Aug 11, 2017 at 06:51 AM ·
2dmovement
Unity2D How to dodge roll?
Hey guys, I tried several days and finally came here. This is Character Moving code and top down view.I want to character doing dodge roll but i have no idea how to do.My code is weird but i need help plz.
public float moveSpeed;
private float currentMoveSpeed;
public float diagonalMoveModifier;
private Animator anim;
private bool rolling;
private bool playerMoving;
private Vector2 lastMove;
private Rigidbody2D myRigidbody;
void Start () {
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update () {
playerMoving = false;
if (rolling == false)
{
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed,
myRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed);
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
}
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f && Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f)
{
currentMoveSpeed = moveSpeed * diagonalMoveModifier;
}
else
{
currentMoveSpeed = moveSpeed;
}
}
if (Input.GetKeyDown(KeyCode.A) && playerMoving == true && rolling == false)
{
rolling = true;
}
Comment
Your answer
Follow this Question
Related Questions
2D animation transitions and unexpected position changes 2 Answers
Boss AI Help in a 2D Platform game 0 Answers
Need to make grid movement more smooth 1 Answer
Make a character face left. without flip 1 Answer
Sync movements Photon 0 Answers