- Home /
unity 2d platformer dash attack.
hello everyone, i am working on a 2d platformer and have done a lot of work regarding player movement (i aint a programmer, i am learning c# as i am working, changed my player movement script 4 times by now because of learning and experimenting and i am happy with it). currently i am stuck at dash attack. tried various youtube tutorials but cant seem to help. what i am looking for is, player facing left or right, when the dash key is pressed, it should do a sprint/dash for small distance in that direction and kill/damage enemies coming its way, the sprint should be available after set time only so it cannot be spammed. if anyone of you have any spare script or can point me at some free scripts library or anything, that would be great. it is my 1st time ever i am working on a game dev project, all because of my passion. thank you already in advance.
Answer by dignus1993 · Oct 18, 2020 at 11:06 PM
In my code I have an object Player with a script PlayerMovement.cs attached to it. The enemy has a script EnemyHealthManager.cs with a public variable health that is equal to his actual healthpoint.
In my script PlayerMovement you have a function dash function, in order to adjust the parameter of your script change these variables:
speed is the speed of the player when he moves
dashSpeed is the speed of the dash;
dashCountThreshold is the treshold that currentDashSpeed has to underpass in order to stop dashing (dashCountThreshold must be < dashSpeed);
cooldownDash is the time to pass before pressing again the dodge button;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private float dashTimer;
private Vector3 moveDirection;
private Vector3 lastMoveDirection;
private Vector3 dashDirection;
private float currentDashSpeed;
public float speed;
public float dashSpeed;
public float dashCountThreshold;
public float cooldownDash;
public enum State
{
Normal,
Dashing,
}
private State state;
void Start()
{
speed = 10;
dashSpeed = 100;
dashCountThreshold = 60;
cooldownDash = 50;
dashTimer = 0;
rb = GetComponent<Rigidbody2D>();
state = State.Normal;
}
void FixedUpdate()
{
dashTimer--;
switch (state)
{
case State.Normal:
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical")).normalized;
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
lastMoveDirection = moveDirection;
rb.velocity = moveDirection * speed;
}
if (Input.GetButtonDown("Dodge"))
{
if (dashTimer <= 0)
{
dashDirection = lastMoveDirection;
currentDashSpeed = dashSpeed;
dashTimer = cooldownDash;
state = State.Dashing;
}
}
break;
case State.Dashing:
currentDashSpeed -= currentDashSpeed * Time.deltaTime;
rb.velocity = dashDirection * currentDashSpeed;
if (currentDashSpeed < dashCountThreshold)
{
state = State.Normal;
dashTimer = cooldownDash;
}
break;
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag.Equals("Enemy"))
other.gameObject.GetComponent<EnemyHealthManager>().health -= 1;
}
}
Your answer
Follow this Question
Related Questions
Timed Attack System 1 Answer
How can I make an event happen every 2 seconds in the OnTriggerStay void? 2 Answers
How do I fix an issue with my enemy instantly dying? 1 Answer
How to do a "dash"-movement, set distance on key-input? 0 Answers
How Do I Make A Dash&Attack Move For My Top-DownRPG 2D? 0 Answers