- Home /
Character Controller Movement With WASD
i Have Been Looking For Character Controllers That Use WASD For Movement Instead Of An Axis, And Can Not Find One I Need One Pretty Much Like This One
using UnityEngine;
using System.Collections;
public class ScurgeofShadowsController : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
if(CombatSystemScurge.PublicHealth <= 10) {
speed = 1.5f;
}
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
But Uses WASD. Thanks!
Unity can bind keys to an axis. Per the manual, default bindings include: "Horizontal and Vertical are mapped to w, a, s, d and the arrow keys."
Well I Will Have It So It Depends On $$anonymous$$eyCodes That I Change In Game, As I Am Not A Fan Of Unitys Startup Thing Where You Setup Graphics And Input And Stuff, To $$anonymous$$e Its Ugly No Offence Unity.
In that case, you can use Input.Get$$anonymous$$ey() to check if a particular $$anonymous$$eyCode is pressed. You can calculate an appropriate moveDirection
by adding Vector3.forward
, Vector3.back
, Vector3.left
, and Vector3.right
(see script reference) based on the particular keys that are currently pressed.
Thank You! If You Post That As An Answer, I Can $$anonymous$$ark This As Answered!
Answer by MasterChief333 · Dec 01, 2014 at 09:04 PM
I didn't write this, but I need some rep, and I saw that if it is posted, you'll mark it a answered.
Blockquote
In that case, you can use Input.GetKey() to check if a particular KeyCode is pressed. You can calculate an appropriate moveDirection by adding Vector3.forward, Vector3.back, Vector3.left, and Vector3.right (see script reference) based on the particular keys that are currently pressed. -rutter
-CaliebBlockquote