- Home /
CharacterController.Move Not Corresponding to gameobject.transform.rotation
I'm trying to create my own FPS controller and I'm using CharacterController.Move() to move. However it isn't corresponding to the gameobject's transform.rotation like I was expecting since I've assigned the gameobject's transform.rotation = Quaternion.Euler(rot). The characterController moves but only along Input.GetAxis("Horizontal") and Input.GetAxis("Vertical"). Does CharacterController.Move() operate independently from a gameobject's transform.rotation? That's what I think is happening. How would I be able to get the CharacterController to move while also following the gameobject's transform.rotation?
using UnityEngine;
using System.Collections;
public class FirstPersonController : MonoBehaviour {
public Vector2 rot;
public Vector3 movement;
public float movementSpeed = 5.0f;
public float mouseSensitivity = 5.0f;
public float jumpAmount = 5.0f;
public float clampAngle = 90.0f;
public CharacterController characterController;
// Update is called once per frame
void Update () {
// Rotation
rot.y += Input.GetAxis("Mouse X") * mouseSensitivity;
rot.x += -Input.GetAxis("Mouse Y") * mouseSensitivity;
rot.x = Mathf.Clamp(rot.x, -clampAngle, clampAngle);
transform.rotation = Quaternion.Euler(rot);
movement.y += Physics.gravity.y * Time.deltaTime;
if( characterController.isGrounded && Input.GetButton("Jump") ) {
movement.y = jumpAmount;
}
movement = new Vector3(Input.GetAxis("Horizontal") * movementSpeed, movement.y, Input.GetAxis("Vertical") * movementSpeed);
characterController.Move(movement * Time.deltaTime );
}
}
Answer by ninja_gear · May 28, 2016 at 11:59 PM
The .Move() method takes absolute movement, so you need to rotate the input by your transform's rotation before attempting to move.
movement = movement * transform.rotation;
characterController.Move(movement * Time.deltaTime );
Okay so now my character is moving backwards on it's own without any keyboard input. I assume this is due to characterController.$$anonymous$$ove now including transform.rotation for it's values along with keyboard inputs. How do I include only the keyboard inputs for movement but also have characterController.$$anonymous$$ove correspond with the gameobject's transform.rotation?
movement = transform.rotation * movement;
characterController.$$anonymous$$ove(movement * Time.deltaTime );
edit:
if (movement.magnatude != Vector3.zero)
{
movemovement = transform.rotation * movement;
characterController.$$anonymous$$ove(movement * Time.deltaTime );
}
The charactercontroller is still moving with the mouse.
if (movement.magnitude != 0)
{
movement = transform.rotation * movement;
characterController.$$anonymous$$ove(movement * Time.deltaTime);
}