Sorry for the chaotic topic name.
I’m struggling with a proper rotation of one of my sprites.
I don’t know why but every time the sprite stop its movement, rotation starts to move a little. I hope that below .gif will help you understand what I mean.
https://giphy.com/gifs/3oEjHBqCLfCfIQl08U
As you can see rotation is working fine till I stop moving. Then, sprite is slightly rotating which drives me crazy as it collides with my game’s main concept.
float vertMov = Input.GetAxis(“Vertical”);
float horMov = Input.GetAxis(“Horizontal”);
if(vertMov != 0 || horMov != 0)
{
movement.x = vertMov;
movement.y = horMov;
float angle = Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg;
if(vertMov != 0 && horMov != 0)
{
if(movement.magnitude > 1)
{
movement = movement.normalized;
}
}
Quaternion q = Quaternion.AngleAxis(angle, Vector3.back);
Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
viewPos.x = Mathf.Clamp01(viewPos.x);
viewPos.y = Mathf.Clamp01(viewPos.y);
transform.position = Camera.main.ViewportToWorldPoint(viewPos);
transform.Translate(Vector2.up * movement.sqrMagnitude * speed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, q, rotationSpeed * Time.deltaTime);
}