- Home /
Problems with KeyCodes
So I'm trying to get sprinting in with LeftShift in my movement thing.
var rotSpeed = 64;//setting the speed the character will rotate
var moveSpeed = 6;//setting the speed the character will walk
var DefaultMoveSpeed = 6;
private var motor : CharacterMotor;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
var v3 = Vector3 (0.0, Input.GetAxis ("Horizontal"), 0.0);
transform.Rotate (v3 * rotSpeed * Time.deltaTime);
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = moveSpeed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
moveSpeed = 10;
}else
{
moveSpeed = DefaultMoveSpeed;
}
}
What unity is telling me is that LeftShift doesn't exist, even if I change the if line to this:
if (Input.GetKeyDown("LeftShift"))
it still tells me "Input Key named: LeftShift is unknown."
What am I doing wrong here?
Answer by TheIncognito · Feb 08, 2014 at 10:03 PM
Easy.
You don't use Quotes for KeyCodes, you use if(Input.GetKeyDown(KeyCode.LeftShift)
I tried that, as shown by the first bit of code. It still said $$anonymous$$eyCode.LeftShift was unknown.
Answer by Eric5h5 · Feb 08, 2014 at 11:28 PM
As for the first method, don't name your scripts the same thing as Unity classes (or enums). That is, use some other script name instead of KeyCode since that confuses things. As for the second method, you should generally not use strings, but if you did, then the key names are here; there's no "LeftShift" (it's "left shift").