Unity 2D旋转不流畅
Unity 2D rotation not smooth
不向左旋转"fluid, smooth"。左角转动但不流畅和平滑。 角之间没有角。
右边没问题。问题出在左侧。右侧的转弯很平滑。左侧的转角不 流畅,平滑 。
看题:https://youtu.be/kuBWoF5r2Bs
Gun Sprite C# 代码:
public GameObject projectile;
public Camera cam;
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 (angle > 0f && angle < 100f || angle < 0f && angle > -90f)
{
//Reverse
Vector3 theScale = transform.localScale;
theScale.x = 1;
transform.localScale = theScale;
//Limit
angle = Mathf.Clamp(angle, -24, 24);
Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = target;
}
if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)
{
//Reverse
Vector3 theScale = transform.localScale;
theScale.x = -1;
transform.localScale = theScale;
//Limit
angle = Mathf.Clamp(-angle, -24, 24);
Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = target;
}
}
}
在此声明中:
if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)
唯一通过陈述的角度是那些大于 100 或小于 -90 的角度。第一个范围内的所有数字都大于 25,而第二个范围内的所有数字都小于 -25....因此该值将始终变为 25 或 -25。
在第二个语句中,尝试从角度中添加或减去一些东西,直到获得所需范围内的值。也许是这样的:
if (angle > 100) angle -= 100;
if (angle < -90) angle += 90;
不向左旋转"fluid, smooth"。左角转动但不流畅和平滑。 角之间没有角。
右边没问题。问题出在左侧。右侧的转弯很平滑。左侧的转角不 流畅,平滑 。
看题:https://youtu.be/kuBWoF5r2Bs
Gun Sprite C# 代码:
public GameObject projectile;
public Camera cam;
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 (angle > 0f && angle < 100f || angle < 0f && angle > -90f)
{
//Reverse
Vector3 theScale = transform.localScale;
theScale.x = 1;
transform.localScale = theScale;
//Limit
angle = Mathf.Clamp(angle, -24, 24);
Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = target;
}
if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)
{
//Reverse
Vector3 theScale = transform.localScale;
theScale.x = -1;
transform.localScale = theScale;
//Limit
angle = Mathf.Clamp(-angle, -24, 24);
Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = target;
}
}
}
在此声明中:
if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)
唯一通过陈述的角度是那些大于 100 或小于 -90 的角度。第一个范围内的所有数字都大于 25,而第二个范围内的所有数字都小于 -25....因此该值将始终变为 25 或 -25。
在第二个语句中,尝试从角度中添加或减去一些东西,直到获得所需范围内的值。也许是这样的:
if (angle > 100) angle -= 100;
if (angle < -90) angle += 90;