- Home /
Dash and player script not working together!
I have a dash script but it is not working properly, if you try to dash left or right it doesnt work properly like up and down. The problem is the Player movement script but i do not know how to fix it. Here are both scripts:
Dash Script =
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DashAbility : MonoBehaviour { private Rigidbody2D rb; public float DashSpeed; public float DashTime; public float StartDashTime; private int Direction;
public void Start()
{
rb = GetComponent<Rigidbody2D>();
DashTime = StartDashTime;
}
public void Update()
{
if (Direction == 0)
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Direction = 1;
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
Direction = 2;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
Direction = 3;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Direction = 4;
}
}
else
{
if(DashTime <= 0)
{
Direction = 0;
DashTime = StartDashTime;
rb.velocity = Vector2.zero;
}
else
{
DashTime -= Time.deltaTime;
if (Direction == 1)
{
rb.velocity = Vector2.left * DashSpeed;
} else if(Direction == 2)
{
rb.velocity = Vector2.right * DashSpeed;
} else if(Direction == 3)
{
rb.velocity = Vector2.up * DashSpeed;
} else if(Direction == 4)
{
rb.velocity = Vector2.down * DashSpeed;
}
}
}
}
}
Here is the player movement script :
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))] public class Player : MonoBehaviour { public float movementSpeed = 10f;
Rigidbody2D rb;
float movement = 0f;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
movement = Input.GetAxis("Horizontal") * movementSpeed;
}
void FixedUpdate()
{
Vector2 velocity = rb.velocity;
velocity.x = movement;
rb.velocity = velocity;
}
}
Your answer
Follow this Question
Related Questions
Cannot dash in left-down, left-up and right-down directions? 2 Answers
Smooth 3D Dodge Roll Animation 0 Answers
Why does my character only dash forward and not in the moving direction? 1 Answer
Ghost Trail - Creating Sequence of Images that move on players position 0 Answers
,I am trying to code a dash in c#. I cant get it, help! 1 Answer