Hi, I have a script for a character controller. Currently it works but when I let go of a key it sets the rotation back to 0, which considering my script it SHOULD do. I don’t know what to put there instead.
Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), >>>HERE<<<, Input.GetAxis("Vertical"));
Because having that means if I put 0f in the >>>HERE<<< section then it will set rotateY back to 0 each update. What do I put there that will retain it’s rotation on Y? I have tried so many things.
Here is the full code. Please take a look, thanks very much.
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour
{
public float runSpeed = 5f;
public float turnSpeed = 20f;
public float gravity = 21f;
public float jumpSpeed = 12f;
private Vector3 moveVector = Vector3.zero;
public CharacterController CharacterController;
void Awake()
{
CharacterController = gameObject.GetComponent("CharacterController") as CharacterController;
}
void Update()
{
ProcessMovement();
}
void ProcessMovement()
{
moveVector = new Vector3(Input.GetAxis("Horizontal"), moveVector.y, Input.GetAxis("Vertical"));
if(CharacterController.isGrounded Input.GetButton("Jump"))
{
moveVector.y = jumpSpeed;
}
ProcessGravity();
Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), Quaternion.identity.y, Input.GetAxis("Vertical"));
Quaternion lookRotation = Quaternion.LookRotation(currentRotation);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
CharacterController.Move(moveVector * Time.deltaTime);
}
void ProcessGravity()
{
if(!CharacterController.isGrounded)
{
moveVector.y -= gravity * Time.deltaTime;
}
}
}
If I think about it some more it’s not SETTING the rotation.y to 0f, it’s not affecting the rotation.y at all because it’s 0f. Which means it SHOULDNT rotate to face the front each time… but it is.
So why would this cause the player to rotate forwards each time?
One thing I noticed is in the LookRotation function your suposed to use direction vectors not rotations. Maybe try Quaternion.Euler(currentRotation) instead. Or maybe transform.Rotate(currentRotation,Space.Self).