- Home /
Diagonal Dash
I've made a movement script that includes dashes, but I can't dash diagonally. Does anyone know how to include this? Any help is appreciated.
void Update () {
if(DashCD>0){
DashCD -= Time.deltaTime;
}
if(DashCD < 0){
DashCD = 0;
}
if(Input.GetKey(KeyCode.UpArrow)) {
if(Input.GetKeyDown(KeyCode.LeftShift) && DashCD == 0){
GetComponent<Rigidbody2D>().AddForce(Vector2.up * DashSpeed, ForceMode2D.Impulse);
DashCD = DashCDTime;
}
else {
GetComponent<Rigidbody2D>().transform.position += Vector3.up * speed * Time.deltaTime;
}
}
if(Input.GetKey(KeyCode.DownArrow)) {
if(Input.GetKey(KeyCode.LeftShift) && DashCD == 0){
GetComponent<Rigidbody2D>().AddForce(Vector2.down * DashSpeed, ForceMode2D.Impulse);
DashCD = DashCDTime;
}
else {
GetComponent<Rigidbody2D>().transform.position += Vector3.down * speed * Time.deltaTime;
}
}
if(Input.GetKey(KeyCode.LeftArrow)) {
if(Input.GetKey(KeyCode.LeftShift) && DashCD == 0){
GetComponent<Rigidbody2D>().AddForce(Vector2.left * DashSpeed, ForceMode2D.Impulse);
DashCD = DashCDTime;
}
else {
GetComponent<Rigidbody2D>().transform.position += Vector3.left * speed * Time.deltaTime;
}
}
if(Input.GetKey(KeyCode.RightArrow)) {
if(Input.GetKey(KeyCode.LeftShift) && DashCD == 0){
GetComponent<Rigidbody2D>().AddForce(Vector2.right * DashSpeed, ForceMode2D.Impulse);
DashCD = DashCDTime;
}
else {
GetComponent<Rigidbody2D>().transform.position += Vector3.right * speed * Time.deltaTime;
}
}
}
Answer by Harinezumi · Mar 12, 2018 at 02:14 PM
You need to also check if the other arrow is also pressed with boolean AND. However, with the current structure of your code that will be quickly complicated. Instead, let's restructure it using a local variable:
private Rigidbody2D ownRigidbody;
// store the Rigidbody at start to make it more efficient
void Awake() { ownRigidbody = GetComponent<Rigidbody2D>(); }
void Update() {
if (DashCD>0) { DashCD -= Time.deltaTime; }
if (DashCD<0) { DashCD = 0; }
Vector3 direction = Vector3.zero;
if (Input.GetKey(KeyCode.UpArrow)) { direction += Vector2.up; }
if (Input.GetKey(KeyCode.DownArrow)) { direction += Vector2.down; }
if (Input.GetKey(KeyCode.LeftArrow)) { direction += Vector2.left; }
if (Input.GetKey(KeyCode.Rightarrow)) { direction += Vector2.right; }
if (direction == Vector3.zero) { return; } // don't do anything if direction is zero
direction.Normalize();
if (DashCD == 0 && Input.GetKey(KeyCode.LeftShift)) {
ownRigidbody.AddForce(direction * DashSpeed, ForceMode2D.Impulse);
DashCD = DashCDTime;
}
else {
ownRigidbody.transform.position += direction * speed * Time.deltaTime;
// NOT recommended to directly modify position of rigidbody; instead use the below:
ownRigidbody.MovePosition(ownRigidbody.transform.position + direction * speed * Time.deltaTime);
}
}
Basically, first get what keys are being pressed, store them in direction
, then apply the movement using it.
However, with this approach you will probably run into the problem that both keys have to be pressed before you hit shift, that is you need precision control. There are different ways to handle this, but that is a bit more complicated to explain.
Your answer
Follow this Question
Related Questions
Diagonal speed too fast with 0.7 multiplier 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Get result (force & torque) of AddForceAtPosition? 2 Answers
C# the position of the object 1 Answer