Skip to content

Instantly share code, notes, and snippets.

@TheAllenChou
Last active November 20, 2019 07:12
Show Gist options
  • Save TheAllenChou/9a126c74a32e21ae606c124fabd0783a to your computer and use it in GitHub Desktop.
Save TheAllenChou/9a126c74a32e21ae606c124fabd0783a to your computer and use it in GitHub Desktop.
Vector3 inputVec = Vector3.zero;
if (Input.GetKey(KeyCode.UpArrow))
inputVec += Vector3.up;
if (Input.GetKey(KeyCode.DownArrow))
inputVec += Vector3.down;
if (Input.GetKey(KeyCode.LeftArrow))
inputVec += Vector3.left;
if (Input.GetKey(KeyCode.RightArrow))
inputVec += Vector3.right;
Vector3 inputMagSqr = inputVec.sqrMagnitude;
Vector3 inputDir =
inputMagSqr > Mathf.Epsilon
? inputVec / Mathf.Sqrt(inputMagSqr)
: Vector3.zero;
Vector3 moveVec = speed * inputDir * Time.deltaTime;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment