- Home /
Character movement with rotation
So what I'm trying to accomplish is a basic character controller where you use A/D or left/right to rotate, and W/S or up/down to move forward and backward. So far, all I have is the rotation script.
var rotSpeed = 6;//setting the speed the character will rotate
var moveSpeed = 6;//setting the speed the character will walk
function Update()
{
var v3 = Vector3 (Input.GetAxis ("Horizontal"), 0.0);
transform.Rotate (v3 * rotSpeed * Time.deltaTime);
}
How should I go about making the character move forward/backward based on their current rotation?
Answer by aldonaletto · Oct 20, 2013 at 01:45 AM
You could use Translate:
var rotSpeed = 6;//setting the speed the character will rotate
var moveSpeed = 6;//setting the speed the character will walk
function Update()
{
var v3 = Vector3 (Input.GetAxis ("Horizontal"), 0.0);
transform.Rotate (v3 * rotSpeed * Time.deltaTime);
v3 = Vector3 (0.0, 0.0, Input.GetAxis("Vertical"));
transform.Translate (v3 * moveSpeed * Time.deltaTime);
}
But the vector v3 seems wrong - in order to rotate about Y, it should be:
var v3 = Vector3 (0.0, Input.GetAxis ("Horizontal"), 0.0);
NOTE: Moving a simple collider with Translate makes it pass through other colliders. If you want the object to be constrained by other colliders, it should have at least a Rigidbody attached. The behaviour would be somewhat weird, however: the object would shake a lot when trying to pass through a collider, and could even succeed if the collider was thin enough. The CharacterController would be the best alternative - attach a CharacterController to your character and try the example script of SimpleMove (SimpleMove applies gravity automatically).
Your answer
Follow this Question
Related Questions
Character Rotation 2 Answers
How to rotate Character depending on the inclination of the platform or its parts? 1 Answer
Character MoveLook rotation locked? 1 Answer
2D sprite rotating in 3D plane 0 Answers
Help with Character Controller 1 Answer