- Home /
Question by
SimonDenton · Apr 09, 2012 at 10:37 AM ·
c#rotationmovementinput
Player Movement - Non Strafing Quaternion Method
Many games involve directional playermovement without strafing. Instead we see character models rotating to face the direction of the input and move in that direction. In this script I incorporate the simple Move script within character controller and I added additional code to rotate the character model towards the direction of input.
void Update ()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) { //If we're on the ground
if(movementEnabled) //When we're allowed to move
{
moveDirection = new Vector3(Input.GetAxis("Left/Right"), 0, Input.GetAxis("Down/Up"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= skateSpeed;
transform.rotation *= Quaternion.AngleAxis(new Vector3(Input.GetAxis("Left/Right"), 0, 0));
Currently the transform.rotation has the error " No overload for method AngleAxis' takes
1' arguments". However, it is still unclear to me for how to use quaternion for this commonly used method of player movement.
Thanks for the help!
Comment
Best Answer
Answer by SimonDenton · Apr 21, 2012 at 11:52 AM
I managed to find a script close enough to what I was aiming which did not require use of Quaternion. Thanks to VS48!
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) { //If we're on the ground
if(movementEnabled) //When we're allowed to move
{
moveDirection = new Vector3(Input.GetAxis("Left/Right"), 0, Input.GetAxis("Down/Up"));
moveDirection *= skateSpeed;
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Left/Right"), 0f, Input.GetAxis("Down/Up")));
}
}