Unity 2D rotation not smooth?
There's something wrong with the rotation of the gun. He's doing -1 on the X-axis. No problem on the other rotation X 1 axis. I suggest you watch a video.
No problem in the right part. The problem is on the left side. The turns on the right side are smooth. Turning angles on the left side are not smooth.
Watch the problem: https://youtu.be/kuBWoF5r2Bs
C# Code:
void FixedUpdate()
{
//rotation
Vector3 mousePos = Input.mousePosition;
Vector3 objectPos = cam.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
//transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); //Rotating!
if (Input.GetButtonDown("Fire1"))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
}
if (angle > 0f && angle < 100f || angle < 0f && angle > -90f)
{
if (direction == false)
{
direction = true;
Dondurs();
}
}
if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)
{
if (direction == true)
{
direction = false;
Dondurs();
}
}
if (direction == true)
{
angle = Mathf.Clamp(angle, -24, 24);
Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = target;
}
if (direction == false)
{
angle = Mathf.Clamp(angle, 24, -24);
Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = target;
}
}
public void Dondurs()
{
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
Comment