Unity中获取到⾯板上的Rotation值
the helpUnity中的旋转分为四元数(x,y,z,w)和欧拉⾓。
使⽤ation.z获取到的是⼀个Cos,Sin的值,⽽不是⾯板中的值。
⽽使⽤transform.localEulerAngles.z是⼀个0-360的值,当正向旋转时可以获取到⾯板上对应的数值,但反向旋转时数值就会从360⼀
直往下减少。
这部分涉及到数学知识,我也不是太懂。以后再进⾏补充!
利⽤Unity的反射可以获取到unity⾯板中对应的Rotation数值:
audio technica垃圾桶英文//获取到旋转的正确数值allison
public Vector3 GetInspectorRotationValueMethod(Transform transform)
{
// 获取原⽣值
System.Type transformType = transform.GetType();
PropertyInfo m_propertyInfo_rotationOrder = transformType.GetProperty("rotationOrder", BindingFlags.Instance | BindingFlags.NonPublic);
object m_OldRotationOrder = m_propertyInfo_rotationOrder.GetValue(transform, null);
MethodInfo m_methodInfo_GetLocalEulerAngles = transformType.GetMethod("GetLocalEulerAngles", BindingFlags.Instance | BindingFlags.NonPublic); object value = m_methodInfo_GetLocalEulerAngles.Invoke(transform, new object[] { m_OldRotationOrder });
string temp = value.ToString();
//将字符串第⼀个和最后⼀个去掉
temp = temp.Remove(0, 1);
temp = temp.Remove(temp.Length - 1, 1);
//⽤‘,’号分割
string[] tempVector3;
tempVector3 = temp.Split(',');
//将分割好的数据传给Vector3
Vector3 vector3 = new Vector3(float.Par(tempVector3[0]), float.Par(tempVector3[1]), float.Par(tempVector3[2]));
return vector3;
}
——————————⼩案例:让⼀个钩⼦左右来回摇摆
⼀:⽅法1
利⽤反射得到⾯板中的Rotation值,直接判断Rotation值的范围就可以
using System.Reflection;
using UnityEngine;
大学英语//摇摆⽅向
public enum SwingDir
{
take my breath awayRIGHT,
LEFT,
}
public class Hook : MonoBehaviour
{
public float swingSpeed;//摇摆的速度
private SwingDir swingDir;//摇摆⽅向
private void Update()
{
//判断旋转⽅向
switch (swingDir)
{
ca SwingDir.RIGHT:
transform.Rotate(Vector3.forward * Time.deltaTime * swingSpeed);
if (GetInspectorRotationValueMethod(transform) > 90)
{
swingDir = SwingDir.LEFT;
}
break;
ca SwingDir.LEFT:
transform.Rotate(Vector3.forward * Time.deltaTime * -swingSpeed);
if (GetInspectorRotationValueMethod(transform) < -90)
圣诞节英语怎么说{
swingDir = SwingDir.RIGHT;
}
break;
}
}
//获取到⾯板上对应的Rotation数值
public float GetInspectorRotationValueMethod(Transform transform)
{
// 获取原⽣值
System.Type transformType = transform.GetType();
PropertyInfo m_propertyInfo_rotationOrder = transformType.GetProperty("rotationOrder", BindingFlags.Instance | BindingFlags.NonPublic);
object m_OldRotationOrder = m_propertyInfo_rotationOrder.GetValue(transform, null);
MethodInfo m_methodInfo_GetLocalEulerAngles = transformType.GetMethod("GetLocalEulerAngles", BindingFlags.Instance | BindingFlags.NonPublic); object value = m_methodInfo_GetLocalEulerAngles.Invoke(transform, new object[] { m_OldRotationOrder });
string temp = value.ToString();
//将字符串第⼀个和最后⼀个去掉
temp = temp.Remove(0, 1);
bumpertemp = temp.Remove(temp.Length - 1, 1);
//⽤‘,’号分割
string[] tempVector3;
tempVector3 = temp.Split(',');
//将分割好的数据传给Vector3
Vector3 vector3 = new Vector3(float.Par(tempVector3[0]), float.Par(tempVector3[1]), float.Par(tempVector3[2]));
return vector3.z;
}
}
⼆:⽅法2
声明⼀个float类型的变量(充当累加器的功能),将这个值⼀直赋给旋转的⾓度
using UnityEngine;
//摇摆⽅向
public enum SwingDir
samp
{
RIGHT,
LEFT,
}
public class Hook : MonoBehaviour
{
private float currentAngle;//⾓度
public float swingSpeed;//摇摆的速度
private SwingDir swingDir;//摇摆⽅向
private void Update()
{kuso什么意思
//判断旋转⽅向
switch (swingDir)
{
ca SwingDir.RIGHT:
currentAngle += Time.deltaTime * swingSpeed;
{
swingDir = SwingDir.LEFT;
}
break;
ca SwingDir.LEFT:
currentAngle -= Time.deltaTime * swingSpeed;
{
swingDir = SwingDir.RIGHT;
}
break;
}
}
}
三:⽅法3
利⽤向量的点乘获取到夹⾓,⽤夹⾓判断是否到达边界
public class Hook: MonoBehaviour
{
int unit = 1;//判断正负的⼀个数值
void Update()
{
float dot = Vector3.Dot(-transform.up, Vector3.down);
if (dot < 0.707f)
{
unit = -unit;
}
transform.Rotate(Vector3.forward, dot * unit);
}
}