- Home /
How to stop flipping the Y rotation back to zero?
Hey guys,
I am making a player movement and freelook camera. At the moment when the player stands still you can look around the player, when the player moves you can also change direction of the player movement with the camera.
How ever when you stand still the player Y rotation will be flipped back to zero. What I want is to make the player look forward with his last Y rotation value of the movement. Any idea how not make the Y rotation flipping back to zero when player stands stile, while meantaning the freelook around the player when movement <= 0.1f?
public class PlayerController : MonoBehaviour
{
public float speed;
public float rotationSpeed;
private Rigidbody m_Rb;
private Vector3 m_Movement;
private Camera m_MainCamera;
private void Awake()
{
m_Rb = GetComponent<Rigidbody>();
m_MainCamera = Camera.main;
}
void FixedUpdate()
{
float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
Debug.Log(m_Movement.magnitude);
m_Movement.Set(horizontalInput, 0, verticalInput);
m_Movement.Normalize();
{
Quaternion camRotation = m_MainCamera.transform.rotation;
Vector3 targetDirection = camRotation * m_Movement;
targetDirection.y = 0;
m_Rb.MovePosition(m_Rb.position + targetDirection.normalized * speed * Time.fixedDeltaTime);
m_Rb.MoveRotation(Quaternion.Euler(0, camRotation.eulerAngles.y, 0));
if (m_Movement.magnitude <= 0.1f)
{
m_Rb.MoveRotation(Quaternion.Euler(0, 0, 0));
}
}
}
}
also ( <= ) means smaller or equal, you need here bigger or equal ( >= ) so it updates only when you're moving also you can see in my code below i just did ( != vector2.zero ) so it only updates when i move
Answer by IvanBgd · Feb 19 at 01:01 PM
i also had this problem, this code from my game might help:
note: it only updates the rotation when moving
private void FixedUpdate()
{
move = new Vector2(js.Horizontal, js.Vertical); //js is joystick
rb.velocity = new Vector2(move.x * ms, move.y * ms);
if (move != Vector2.zero) //this is what you need
{
transform.up = move;
}
}
Your answer
Follow this Question
Related Questions
How to make my Camera Controller look at target 0 Answers
Multiple Cars not working 1 Answer
How to make the player direction change with the camera rotation? 1 Answer
Distribute terrain in zones 3 Answers
my screen keeps on jittering when I play what is wrong? here is my camera and movement script 1 Answer