Skip to content

Instantly share code, notes, and snippets.

@5outh
Created Jul 11, 2012
Embed
What would you like to do?
Move Character
#pragma strict
var forwardSpeed = 10;
var backwardSpeed = 8;
var sideSpeed = 10;
var xDisplacement:float;
var zDisplacement:float;
var displacement:Vector3;
function Update () {
if(Input.GetKey("w")){
//up
xDisplacement = Mathf.Sin(Mathf.Deg2Rad * transform.eulerAngles.y);
zDisplacement = Mathf.Cos(Mathf.Deg2Rad * transform.eulerAngles.y);
displacement = Vector3(xDisplacement, 0, zDisplacement) * forwardSpeed * Time.deltaTime;
transform.position += displacement;
}
if(Input.GetKey("s")){
//down
xDisplacement = Mathf.Sin(Mathf.Deg2Rad * transform.eulerAngles.y);
zDisplacement = Mathf.Cos(Mathf.Deg2Rad * transform.eulerAngles.y);
displacement = Vector3(xDisplacement, 0, zDisplacement) * forwardSpeed * Time.deltaTime;
transform.position -= displacement;
}
if(Input.GetKey("a")){
//left
xDisplacement = Mathf.Sin(Mathf.Deg2Rad * (transform.eulerAngles.y + 90));
zDisplacement = Mathf.Cos(Mathf.Deg2Rad * (transform.eulerAngles.y + 90));
displacement = Vector3(xDisplacement, 0, zDisplacement) * forwardSpeed * Time.deltaTime;
transform.position -= displacement;
}
if(Input.GetKey("d")){
//right
xDisplacement = Mathf.Sin(Mathf.Deg2Rad * (transform.eulerAngles.y + 90));
zDisplacement = Mathf.Cos(Mathf.Deg2Rad * (transform.eulerAngles.y + 90));
displacement = Vector3(xDisplacement, 0, zDisplacement) * forwardSpeed * Time.deltaTime;
transform.position += displacement;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment