- Home /
Clamp rotation of sprite?
Hi,
I'm making a 2d archery game. The player character sprite is made up of many sprites. I'm trying to clamp the rotation of the body sprite of the player so that he looks at where his bow is pointing. I'm having trouble clamping his rotation when flipped.
I can sucessfully clamp when the player is facing right(15-80), but when facing left, the degrees goes from 90 to 0 then further to -345, which cannot seemingly be clamped without glitching.
Here is my code as it stands:
//the vector for the players facing
Vector3 direction = new Vector3();
//the direction of the projectile is the position difference between the target and origin
Vector2 dir = target - _body.transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
if(_player.facingLeft == false)
{
direction = Vector3.forward;
angle = Mathf.Clamp(angle, -15, 80);
}
else if(_player.facingLeft == true)
{
direction = Vector3.back;
angle += 180;
//angle = Mathf.Clamp(angle, min, max); //none of these values seem to clamp
}
//rotate body sprite
_body.transform.rotation = Quaternion.AngleAxis(angle, direction);
Your answer
Follow this Question
Related Questions
Trying to get the angle of a camera object based on players forward, is not distance independent 0 Answers
Find the difference between the Z rotation of two GameObjects 2 Answers
Limiting the rotation of an object 1 Answer
Get current Z rotation of transform in degrees 0 Answers
2D head breaks when flipping character 0 Answers