I am trying to simply character controller without using a rigidbody (since I don't need any physics.) The results are ok, but for some reason my gameObject moves faster when moving diagonally than when just heading in a single direction. Here is my code:
void FixedUpdate() {
float xPos = Input.GetAxis("Horizontal") * _player.speed * Time.deltaTime;
float yPos = Input.GetAxis("Vertical") * _player.speed * Time.deltaTime;
Vector3 newPos = new Vector3(xPos, 0, yPos);
newPos = Camera.main.transform.TransformDirection(newPos);
_position = _position + newPos;
_position.y = _player.transform.position.y;
_direction = new Vector3(xPos, 0, yPos);
_direction = Camera.main.transform.TransformDirection(_direction);
_direction.y = 0;
}
void Update() {
if(_direction.sqrMagnitude > 0.01)
_player.transform.rotation = Quaternion.Slerp (_player.transform.rotation, Quaternion.LookRotation (_direction), Time.deltaTime * 10);
_player.transform.position = Vector3.Slerp(_player.transform.position, _position,Time.deltaTime*10);
}
If anyone has any suggestions as to how I can get around this problem or perhaps even improve the above code, I'd really appreciate it!