- Home /
Question by
NohanG · Aug 19, 2018 at 03:48 AM ·
movementcharactercontrollerdirection
Character floats on backwards walk
I have a script that controls character movement via a character controller ,but for some odd reason my character floats upwards as you use 's' and walk backwards. I think it is probably due to the slight forward tilt of the camera ,but how would I fix this and stop the floating?
The script:
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
//Controls the facing direction of the player
Vector3 moveDirection = new Vector3(moveHorizontal, 0, moveVertical);
moveDirection = cam.TransformDirection(moveDirection);
if(c_Controller.isGrounded == false)
{
moveDirection.y -= gravity * Time.deltaTime;
}
//Move the player
if (!InventoryUI.inventoryOpen)
{
c_Controller.Move(moveDirection * moveSpeed * Time.deltaTime);
if (!Input.GetKey(KeyCode.LeftAlt))
{
character.rotation = Quaternion.Euler(character.eulerAngles.x, cam.eulerAngles.y, character.eulerAngles.z);
}
else if (Input.GetKeyUp(KeyCode.LeftAlt))
{
//Smoothly move camera rotation back to original rotation
}
AnimatorControl(playerAnimator);
}
Comment
To clarify, the character still moves in the negative global z when walking backwards ,but with the floating