我对团结很陌生,我以创建非game2D应用程序的特殊方式使用它所以我只使用UI元素。我对自己想要做的事情有些疑问。我有一个按照手指(或鼠标)旋转的物体,但物体不能离开"区域"这是我现在的代码:
using UnityEngine;
using System.Collections;
public class SteeringWheel : MonoBehaviour
{
public float angleMax = 90.0f;
private float baseAngle = 0.0f;
void OnMouseDown()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
}
void OnMouseDrag()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
Debug.Log("ang1 : " + ang);
ang = Mathf.Clamp(ang, -90, 90);
if (transform.eulerAngles.z >= 89 && transform.eulerAngles.z <= 271)
{
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 0.7f, transform.rotation.w);
return;
}
transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
}
}
这是问题的GIF:
答案 0 :(得分:0)
删除if语句及其中的所有内容,并使用baseAngle将之前的Mathf.Clamp调用调用,而不是之后。
我发现您的代码没有任何问题导致这两项更改无法修复。
(顺便说一下,我假设baseAngle位于方向盘的中心。如果没有,你应该相应地改变你的范围。)
干杯!
答案 1 :(得分:0)
感谢您的回答:) 我在Unity Answer上问我的问题,有人回答我 我解决了这个问题:
public class SteeringWheel : MonoBehaviour
{
public float angleMax = 90.0f;
private float baseAngle = 0.0f;
void OnMouseDown()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
}
void OnMouseDrag()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
//float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
float ang = Mathf.Atan2(Mathf.Max(pos.y, 0), pos.x) * Mathf.Rad2Deg - baseAngle;
Debug.Log("ang1 : " + ang);
ang = Mathf.Clamp(ang, -90, 90);
transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
}
}