Created
February 17, 2016 21:42
Dash script made by gadonj18 in #unity3d irc on freenode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you would attach the script to the player right?