Unity mathf.clamp 多个角度范围
Unity mathf.clamp in multiple angle ranges
我想将箭头移动的区域限制在图中所示的 -140 到 -40 度之间,但是如果我将它限制在 - 140 和 -40 分别作为最小值和最大值,它只能让我将箭头移动到红色区域而不是所需的区域,我的意思是我只想在非红色区域移动箭头,如果有人可以帮助我从这段代码开始,不做太多改动,我将不胜感激,谢谢。
编辑:添加了基于条件的临时代码。
explanatory diagram
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
//rotationZ = Mathf.Clamp(rotationZ, -140, 180);
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
Debug.Log(rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if (myPlayer.transform.eulerAngles.y == 180)
{
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
}
void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition)
- transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) *
Mathf.Rad2Deg;
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -
rotationZ);
}
else if (myPlayer.transform.eulerAngles.y == 180)
{
transform.localRotation = Quaternion.Euler(180, 180, -
rotationZ);
}
}
}
}
private readonly float deadZone = Mathf.Sin(-40 * Mathf.Deg2Rad);
private Vector3 rightDeadZoneEnd = new Vector3(Mathf.Cos(-40 * Mathf.Deg2Rad), Mathf.Sin(-40 * Mathf.Deg2Rad), 0);
private Vector3 leftDeadZoneEnd = new Vector3(Mathf.Cos(-140 * Mathf.Deg2Rad), Mathf.Sin(-140 * Mathf.Deg2Rad), 0);
private void Update()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
direction.Normalize();
if (direction.y < deadZone)
{
if (direction.x >= 0)
{
direction = rightDeadZoneEnd;
}
else
{
direction = leftDeadZoneEnd;
}
}
if (direction.x < 0)
{
transform.localScale = new Vector3(1, -1, 1);
}
else
{
transform.localScale = Vector3.one;
}
direction = Quaternion.Euler(0, 0, 90) * direction;
transform.localRotation = Quaternion.LookRotation(Vector3.forward, direction);
}
我想将箭头移动的区域限制在图中所示的 -140 到 -40 度之间,但是如果我将它限制在 - 140 和 -40 分别作为最小值和最大值,它只能让我将箭头移动到红色区域而不是所需的区域,我的意思是我只想在非红色区域移动箭头,如果有人可以帮助我从这段代码开始,不做太多改动,我将不胜感激,谢谢。
编辑:添加了基于条件的临时代码。
explanatory diagram
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
//rotationZ = Mathf.Clamp(rotationZ, -140, 180);
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
Debug.Log(rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if (myPlayer.transform.eulerAngles.y == 180)
{
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
}
void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition)
- transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) *
Mathf.Rad2Deg;
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -
rotationZ);
}
else if (myPlayer.transform.eulerAngles.y == 180)
{
transform.localRotation = Quaternion.Euler(180, 180, -
rotationZ);
}
}
}
}
private readonly float deadZone = Mathf.Sin(-40 * Mathf.Deg2Rad);
private Vector3 rightDeadZoneEnd = new Vector3(Mathf.Cos(-40 * Mathf.Deg2Rad), Mathf.Sin(-40 * Mathf.Deg2Rad), 0);
private Vector3 leftDeadZoneEnd = new Vector3(Mathf.Cos(-140 * Mathf.Deg2Rad), Mathf.Sin(-140 * Mathf.Deg2Rad), 0);
private void Update()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
direction.Normalize();
if (direction.y < deadZone)
{
if (direction.x >= 0)
{
direction = rightDeadZoneEnd;
}
else
{
direction = leftDeadZoneEnd;
}
}
if (direction.x < 0)
{
transform.localScale = new Vector3(1, -1, 1);
}
else
{
transform.localScale = Vector3.one;
}
direction = Quaternion.Euler(0, 0, 90) * direction;
transform.localRotation = Quaternion.LookRotation(Vector3.forward, direction);
}