- Home /
Dash Movement script works until if statement is added
This script works until the section of code I marked with a comment is added and i can't understand why... I'm thinking about reworking my basic movement controls to something more written out that i know will work but i'd like to have this more simple and graceful code more than something basic. If anyone can tell me what I'm doing wrong that'd be awesome!
public class Player : MonoBehaviour
{
public float speed = 5f;
public Rigidbody2D rb;
[SerializeField]
private float dashMultiplier = 1f;
private Vector3 tempVect;
void Update()
{
/* if (Input.GetButton("Space"))
{
dashMultiplier = 1.3f;
} */
DashCheck(dashMultiplier);
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
tempVect = new Vector2(h, v);
tempVect = tempVect.normalized * speed * Time.deltaTime;
rb.MovePosition(rb.transform.position + tempVect);
if (Input.GetButtonUp("Up"))
{
rb.velocity = Vector3.zero;
}
else if (Input.GetButtonUp("Down"))
{
rb.velocity = Vector3.zero;
}
else if (Input.GetButtonUp("Left"))
{
rb.velocity = Vector3.zero;
}
else if (Input.GetButtonUp("Right"))
{
rb.velocity = Vector3.zero;
}
speed = 5f;
dashMultiplier = 1f;
}
void DashCheck (float dashMultiplier)
{
speed = speed * dashMultiplier;
}
}
Answer by LTonon · Jan 11, 2020 at 05:32 PM
This does not work because you are using GetButton to reference your Space key. GetButton returns true only if the virtual button with the given name is being pressed, and those buttons are defined at the InputManager in ProjectSettings. By default there is no virtual button with the name "Space", if you have created one with this name then the problem is something else, however, I think that if you change your condition inside the if to
if (Input.GetKeyDown(KeyCode.Space))
it should compile correctly, but I still think your dash would be weird.
I recommended GetKeyDown since most dashes in games are done by just pressing the button once, but it seems like your code would only work if the player press and hold the space key (which a GetKey would help)... But in this way the player could use a dash all the time, if I understood it correctly.
Thanks so much for the help, this worked and you’re right the dash is really really weird. I’ve gotta tune it up and change some stuff but thanks for help getting it up and running!
Your answer
![](https://koobas.hobune.stream/wayback/20220612221352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smooth dashing issues 0 Answers
Object doesn't move 2 Answers
Dashing with rigidbody2D not working right 2 Answers
Dash and player script not working together! 0 Answers
Diagonal Dash 1 Answer