- Home /
Help with my Dash move?
Hi, guys. I'm trynig to code a player controller with a Dash function that allows my character to dash from a point to another, by using the "Dash" button that i set at the Input Manager (left shift). I've watched 5 ou 6 YouTube tutorials and none have worked for me.
I did tried some answers here in the Unity Answer section, but i've concluded that something is wrong with my code.
Could someone please help me?
public class PlayerController : MonoBehaviour {
public float speed;
public float jumpForce;
public float secondJumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJump;
public int extraJumpValue;
//Dash tentative
public float startDashTime;
float dashTime;
public float dashSpeed;
public Input dash;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
extraJump = extraJumpValue;
dashTime = startDashTime;
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if(facingRight == true && moveInput < 0)
{
Flip();
}
}
// Update is called once per frame
void Update ()
{
if (isGrounded == true)
{
extraJump = extraJumpValue;
}
Jumping();
Dash();
/*
* "Blink" system, used while dash does not work
* if (Input.GetKeyDown(KeyCode.Z))
{
transform.position += new Vector3(dashSpeed * Time.deltaTime, 0.1f,0f);
}
*/
}
private void Jumping()
{
if (Input.GetButtonDown("Jump") && extraJump > 0)
{
rb.velocity = Vector2.up * secondJumpForce;
extraJump--;
}
if (Input.GetButtonDown("Jump") && extraJump == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void Dash()
{
if (Input.GetButton("Dash"))
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
It would be helpful to tell us what is happening that you don't want. Is the player not moving the direction you want? Is the player not moving at all? tell us what is happening
Answer by myzzie · Aug 20, 2018 at 05:11 AM
I think what you're looking for is
rb.velocity = transform.right * dashSpeed
Well, thanks hahaha. $$anonymous$$y bad.
Also, i was doing it in Update ins$$anonymous$$d of FixedUpdate.
It works now :)