unity,物体旋转后会回再次旋转到相邻设置好的最近角度代码。
发布网友
发布时间:2023-01-16 23:36
我来回答
共2个回答
热心网友
时间:2023-11-18 03:10
//物体位置随鼠标转动,转动范围*在0-36之间
public float rotateSpeed = 10.0f;//旋转速率
public float mouseSpeed = 1.0f;//鼠标移动速率
private Vector3 lastMousePos;
private float minAngel = 0.0f;//允许最小角度
private float maxAngel = 36.0f;//允许最大角度
void Start()
{
lastMousePos = Input.mousePosition;
}
void Update()
{
Quaternion targetQua;
targetQua = Quaternion.Euler(0, (Input.mousePosition - lastMousePos).x * mouseSpeed, 0) * transform.localRotation;
transform.localRotation = Quaternion.Euler(targetQua.eulerAngles.x, Mathf.Clamp(Mathf.Min(targetQua.eulerAngles.y, 360.0f - targetQua.eulerAngles.y), minAngel, maxAngel), targetQua.eulerAngles.z);
lastMousePos = Input.mousePosition;
}
//判断离0,12,24,36哪个最近
float FindNearestAngel(float curAngel)
{
float nearestAngel=0.0f;
if (curAngel >=0.0f && curAngel <6.0f)
{
nearestAngel = 0.0f;
}
else if (curAngel >= 6.0f && curAngel < 18.0f)
{
nearestAngel = 12.0f;
}
else if (curAngel >= 18.0f && curAngel < 30.0f)
{
nearestAngel = 24.0f;
}
else if (curAngel >= 30.0f && curAngel <= 36.0f)
{
nearestAngel = 36.0f;
}
return nearestAngel;
}
//自动转动到最近的角度
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, FindNearestAngel(transform.rotation.eulerAngles.y), 0), Time.deltaTime * rotateSpeed)
热心网友
时间:2023-11-18 03:11
没看懂,要是想做旋转动画可以使用tween插件,或者做成动画就行了,,,,,,是这个意思吧追问具体要求是物体有初始位置随鼠标转动,转动范围是0至36,退出移动状态是判断此时所处角度距离0,12,24和36中4个最相近的角度值,并自动转到该角度。
热心网友
时间:2023-11-18 03:10
//物体位置随鼠标转动,转动范围*在0-36之间
public float rotateSpeed = 10.0f;//旋转速率
public float mouseSpeed = 1.0f;//鼠标移动速率
private Vector3 lastMousePos;
private float minAngel = 0.0f;//允许最小角度
private float maxAngel = 36.0f;//允许最大角度
void Start()
{
lastMousePos = Input.mousePosition;
}
void Update()
{
Quaternion targetQua;
targetQua = Quaternion.Euler(0, (Input.mousePosition - lastMousePos).x * mouseSpeed, 0) * transform.localRotation;
transform.localRotation = Quaternion.Euler(targetQua.eulerAngles.x, Mathf.Clamp(Mathf.Min(targetQua.eulerAngles.y, 360.0f - targetQua.eulerAngles.y), minAngel, maxAngel), targetQua.eulerAngles.z);
lastMousePos = Input.mousePosition;
}
//判断离0,12,24,36哪个最近
float FindNearestAngel(float curAngel)
{
float nearestAngel=0.0f;
if (curAngel >=0.0f && curAngel <6.0f)
{
nearestAngel = 0.0f;
}
else if (curAngel >= 6.0f && curAngel < 18.0f)
{
nearestAngel = 12.0f;
}
else if (curAngel >= 18.0f && curAngel < 30.0f)
{
nearestAngel = 24.0f;
}
else if (curAngel >= 30.0f && curAngel <= 36.0f)
{
nearestAngel = 36.0f;
}
return nearestAngel;
}
//自动转动到最近的角度
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, FindNearestAngel(transform.rotation.eulerAngles.y), 0), Time.deltaTime * rotateSpeed)
热心网友
时间:2023-11-18 03:11
没看懂,要是想做旋转动画可以使用tween插件,或者做成动画就行了,,,,,,是这个意思吧追问具体要求是物体有初始位置随鼠标转动,转动范围是0至36,退出移动状态是判断此时所处角度距离0,12,24和36中4个最相近的角度值,并自动转到该角度。