Skip to content

Instantly share code, notes, and snippets.

@DB-009
Created February 17, 2016 21:42
Show Gist options
  • Save DB-009/2e151660b6505a945210 to your computer and use it in GitHub Desktop.
Save DB-009/2e151660b6505a945210 to your computer and use it in GitHub Desktop.
Dash script made by gadonj18 in #unity3d irc on freenode
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
Vector3 moveDir;
Rigidbody rb;
float speed = 5f;
float dashCooler = 0.5f;
KeyCode dashKey;
bool dashing = false;
Vector3 dashDir;
float dashTimer = 0f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
moveDir = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
moveDir = Vector3.forward;
}
else if (Input.GetKey(KeyCode.S))
{
moveDir = -Vector3.forward;
}
else if (Input.GetKey(KeyCode.A))
{
moveDir = -Vector3.right;
}
else if (Input.GetKey(KeyCode.D))
{
moveDir = Vector3.right;
}
if (Input.GetKeyDown(KeyCode.W))
{
CheckDash(KeyCode.W, Vector3.forward);
}
else if (Input.GetKeyDown(KeyCode.S))
{
CheckDash(KeyCode.S, -Vector3.forward);
}
else if (Input.GetKeyDown(KeyCode.A))
{
CheckDash(KeyCode.A, -Vector3.right);
}
else if (Input.GetKeyDown(KeyCode.D))
{
CheckDash(KeyCode.D, Vector3.right);
}
dashCooler -= Time.deltaTime;
}
void CheckDash(KeyCode key, Vector3 dir)
{
if (dashCooler > 0f && dashKey == key)
{
dashing = true;
dashDir = dir;
dashTimer = 0.5f;
}
else
{
dashKey = key;
dashCooler = 0.5f;
}
}
void FixedUpdate()
{
if (dashing)
{
rb.MovePosition(rb.position + dashDir * speed * 2 * Time.deltaTime);
dashTimer -= Time.deltaTime;
if (dashTimer <= 0f)
{
dashing = false;
}
}
if (moveDir != Vector3.zero)
{
rb.MovePosition(rb.position + moveDir * speed * Time.deltaTime);
}
}
}
@Evangelos7
Copy link

Is there a way to throw in a cooldown?

@DB-009
Copy link
Author

DB-009 commented May 11, 2020

Theres already one dashCooler it's at 0.5 om the top in public variables you can change that as far as adding a visual bar will have to track time left on coolDown and use a fill bar etc.

@shibashroom
Copy link

what do i attach this to and how do i map the actual function

@deadasadodo
Copy link

you would attach the script to the player right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment