- Home /
Character rotating to previous rotation
Hello! I'm pretty new to Unity scripting, but I wanted to start working on a sort of large project. I'm trying to make a third-person Pokemon-like game, where the camera can be rotated in whichever direction, and the player model will turn to face and move in that direction. I got the player model to look in the direction it's heading, but for some reason, as soon as you stop moving, the player model turns to look in the default direction it was when you start the game, if that makes sense. Does anyone know why that might be happening? Here is the code I have so far:
public GameObject player;
public CharacterController controller;
public float playerSpeed = 10.0f;
public float rotationSpeed;
Vector3 move;
private void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//Getting input from player - used for movement
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//Player movement
Vector3 movementDirection = new Vector3(x, 0, z);
movementDirection.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementDirection), 0.15f);
transform.Translate(movementDirection * playerSpeed * Time.deltaTime, Space.World);
//Call sprint
Sprint();
}
void Sprint()
{
//If left shift is pressed, double movement speed. Otherwise, keep it at the default speed
if(Input.GetKeyDown(KeyCode.LeftShift))
{
playerSpeed = playerSpeed * 2;
}
else if(Input.GetKeyUp(KeyCode.LeftShift))
{
playerSpeed = 10.0f;
}
}
Answer by lluisvinent4 · Dec 12, 2021 at 02:17 AM
@Comicbook23 When you are not moving the player, the values of Input.GetAxis are 0, since there is no input, therefore, x and z are also 0 and the player rotates in that direction. Check that x or z are not equal to 0 before set the rotation.
@lluisvinent4 Thank you! That seems to have fixed that problem. There seems to be a couple other problems, though. The character model isn't always turning to the direction it's moving. For example, when I move backwards, the model continues to face in whichever direction it's facing. If the model is facing in any direction other than the default forward direction, and I press W, it will simply move in the default forward direction without turning to face that way. Also, I'm hoping to have the model's "forward" direction be whichever way the camera is facing. Right now, it doesn't matter which way the camera is facing, the model just moves in the global "forward" direction. Is there a way I can change that?
@Comicbook23 If what you want is to make the direction relative to the rotation of the camera, you must multiply the rotation by the direction:
void Update()
{
//Getting input from player - used for movement
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
if (x != 0 || z != 0)
{
//Player movement
Vector3 movementDirection = new Vector3(x, 0, z);
movementDirection.Normalize();
Vector3 directionRelativeToCam = cam.transform.rotation * movementDirection;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(directionRelativeToCam), 0.15f);
transform.Translate(directionRelativeToCam * playerSpeed * Time.deltaTime, Space.World);
//Call sprint
Sprint();
}
}
That's basically done it! The only problem now is that the player can move downward. Like, if the camera is pointing down at all, the player will angle downward and try to move into the floor. Is there a way I can lock the player's z rotation to keep it from doing that? I think it would be the z rotation, but maybe it's y.
Also, the player will spin in a circle if I look to the side. I think it's almost there! If I look to the side, I think the player is constantly updating the direction it's supposed to turn, and since the camera continues to stay in the same spot relative to the player, that makes the player spin in a circle.