if (Input.GetMouseButton(0))
{
Vector3 pos = transform.rectTransform().anchoredPosition;
Vector3 targetPos = Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0);
Vector3 to = targetPos - pos;
float z;
if (targetPos.x > pos.x)
{
z = -Vector3.Angle(Vector3.up, to);
}
else
{
z = Vector3.Angle(Vector3.up, to);
}
transform.localRotation = Quaternion.Euler(0, 0, z);
}
2DUI随鼠标改变Z轴角度
最新推荐文章于 2025-05-13 09:42:01 发布
这段代码演示了在Unity中如何根据鼠标点击位置来计算旋转角度,实现对象的平滑转动。通过获取鼠标位置和对象当前位置的差值,计算出角度并应用到对象的旋转上。
1044

被折叠的 条评论
为什么被折叠?



