1

I was making half 2d ,half 3d game. I wanted the up/down movement to be accompanied by a slight rotation in the direction the player is moving, for that I decided to use Mathf.LerpAngle method , because the documentetion says that the method

returns the shortest path between the specified angles

I also made a flip method which rotates(flips) player

void Flip() { facingRight =!facingRight; transform.Rotate(0,180,0); } } 

And Lerp

input.y = Input.GetAxisRaw("Vertical"); float angle = Mathf.LerpAngle(transform.rotation.x , input.y, Time.deltaTime * rotateSpeed); transform.Rotate(angle, 0,0); 

When starting the game and after flipping the player while moving along the Y axis, LerpAngle causes a 360 degree rotation.

full code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerController : MonoBehaviour { public InputAction MoveAction; private Vector2 input; [SerializeField] float speed; [SerializeField] float rotateSpeed; private bool facingRight; private float inputH; // Start is called before the first frame update void Start() { MoveAction.Enable(); } // Update is called once per frame void Update() { Vector2 move = MoveAction.ReadValue<Vector2>(); Vector2 position = (Vector2)transform.position + move * speed * Time.deltaTime; inputH = Input.GetAxis("Horizontal"); input.y = Input.GetAxisRaw("Vertical"); float angle = Mathf.LerpAngle(transform.rotation.x , input.y, Time.deltaTime * rotateSpeed); transform.Rotate(angle, 0,0); transform.position = position; } void FixedUpdate() { if(inputH > 0 && facingRight) Flip(); else if(inputH < 0 && !facingRight) Flip(); } void Flip() { facingRight =!facingRight; transform.Rotate(0 ,180,0); } } 

I tried: Flip only the player's mesh, but this rotation still does not disappear at start.

I think because of the standard coordinates that are set by start and flip, Lerp Angle does not work as intended.

1
  • This looks really off to me: Mathf.LerpAngle(transform.rotation.x , input.y, ... since input.y is in the range of -1 to 1, and rotation.x is a component of a Quaternion. Commented Jul 15, 2024 at 20:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.