- Home /
Adding a Dash in a 2D sidescroller
I'm new to Unity (3 weeks) So I'm currently making a 2D Sidescroller, got the basic movement (walk, jump and crouch) and combat down, but then I wanted to add a dash movement to my character.
I tried using the rigidbody2D.MovePosition but that just makes my character teleport forward.
Here's my movement script:
void Update(){
if (Input.GetKeyDown(KeyCode.UpArrow))
jump = true;
if (Input.GetKeyDown (KeyCode.DownArrow))
crouch = true;
else if (Input.GetKeyUp (KeyCode.DownArrow))
crouch = false;
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (groundcheck.position, groundedRadius, WhatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetBool ("Crouch", crouch);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
if (Input.GetKeyDown (KeyCode.X))
rigidbody2D.MovePosition(rigidbody2D.position + speed * Time.deltaTime);
if (crouch && grounded) {
jump = false;
x = (crouch ? x * Cx : x);
} else if (!crouch)
x = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (x));
rigidbody2D.velocity = new Vector2 (x * character.MaxSpeed, rigidbody2D.velocity.y);
if (grounded && jump) {
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, character.JumpForce));
}
jump = false;
if (x > 0 && !facingRight)
Flip ();
else if (x < 0 && facingRight)
Flip ();
}
If anyone can help out, Big Thanks from me :)
If you're not playing with acceleration, look into using Lerp to linearly interpolate the velocity of your 2D character to a maximum value, then when the player lets go of the dash key or the movement key or the timer runs out for dashing, linearly interpolate the velocity back to its normal state (normal velocity for running or 0 for stopping movement).
I tried using Lerp, no movement. Also tried using Coroutine, no luck either. Will separating the dash script from the movement script help?
Answer by zaikens · Aug 05, 2014 at 07:16 AM
Instead of MovePosition, you might want to try AddForce.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't move instantiated 2d game oject. 0 Answers
Delayed response: Jumping up from ledge 0 Answers
Check if a variable is true at the beginning of GetButton? 0 Answers