目录
1.transform 旋转:
2.指定旋转到角度
3.指向某个位置
4.自轴旋转
5.绕轴旋转
6.无旋转 (这个物体完全对齐于世界或父轴)
7.限制角度旋转
//=========================================
匀速旋转:以固定速度旋转
float MotorSave;
//物体角度 = 数学类.匀速旋转(物体角度,目标角度,旋转速度*时间系数)
MotorSave = Mathf.MoveTowardsAngle(MotorSave, MotorTarget, MotorSpeed * Time.deltaTime);
///
/// 欧拉角匀速旋转
///
/// 当前欧拉角
/// 目标欧拉角
/// 每帧旋转速度
/// return : 旋转后的欧拉角
public static Vector3 EulerAngles_MoveTowards(Vector3 CurrentEulerAngle, Vector3 TargetEulerAngle, float RotatingMoveSpeed)
{
CurrentEulerAngle.x = Mathf.MoveTowardsAngle(CurrentEulerAngle.x, TargetEulerAngle.x, RotatingMoveSpeed);
CurrentEulerAngle.y = Mathf.MoveTowardsAngle(CurrentEulerAngle.y, TargetEulerAngle.y, RotatingMoveSpeed);
CurrentEulerAngle.z = Mathf.MoveTowardsAngle(CurrentEulerAngle.z, TargetEulerAngle.z, RotatingMoveSpeed);
return CurrentEulerAngle;
}
差值旋转:先快后慢
float MotorSave;
//物体角度 = 数学类.差值旋转(物体角度,目标角度,旋转速度*时间系数)
MotorSave = Mathf.LerpAngle(MotorSave, MotorTarget, MotorSpeed * Time.deltaTime);
///
/// 欧拉角插值旋转
///
/// 当前欧拉角
/// 目标欧拉角
/// 每帧旋转速度(差值比例)
/// return : 旋转后的欧拉角
public static Vector3 EulerAngles_Lerp(Vector3 CurrentEulerAngle, Vector3 TargetEulerAngle, float RotatingMoveSpeed)
{
CurrentEulerAngle.x = Mathf.LerpAngle(CurrentEulerAngle.x, TargetEulerAngle.x, RotatingMoveSpeed);
CurrentEulerAngle.y = Mathf.LerpAngle(CurrentEulerAngle.y, TargetEulerAngle.y, RotatingMoveSpeed);
CurrentEulerAngle.z = Mathf.LerpAngle(CurrentEulerAngle.z, TargetEulerAngle.z, RotatingMoveSpeed);
return CurrentEulerAngle;
}
平滑旋转: 在指定的旋转时间内旋转到目标角度
float MotorSave;
//物体角度 = 数学类.平滑旋转(物体角度,目标角度,返回 每帧速度,旋转时间)
MotorSave = Mathf.SmoothDampAngle(MotorSave, MotorTarget, ref MotorVelocity, MotorSpeed);
///
/// 欧拉角平滑旋转
///
/// 当前欧拉角
/// 目标欧拉角
/// ref 当前速度
/// 到达目标的近似时间
/// return : 旋转后的欧拉角
public static Vector3 EulerAngles_SmoothDamp(Vector3 CurrentEulerAngle, Vector3 TargetEulerAngle, ref Vector3 yVelocity, float MoveTime)
{
CurrentEulerAngle.x = Mathf.SmoothDampAngle(CurrentEulerAngle.x, TargetEulerAngle.x, ref yVelocity.x, MoveTime);
CurrentEulerAngle.y = Mathf.SmoothDampAngle(CurrentEulerAngle.y, TargetEulerAngle.y, ref yVelocity.y, MoveTime);
CurrentEulerAngle.z = Mathf.SmoothDampAngle(CurrentEulerAngle.z, TargetEulerAngle.z, ref yVelocity.z, MoveTime);
return CurrentEulerAngle;
}
//MoveRotation适用于频繁改变,推荐使用这种,较为平滑
//需要获取Rigidbody组件
public Rigidbody playerRgd;
//===========================================
playerRgd.MoveRotation(Quaternion.Slerp(playerRgd.rotation,target,Time.deltaTime*7));
//渐变旋转*速度
playerRgd.MoveRotation(Quaternion.RotateTowards(playerRgd.rotation,target,Time.deltaTime*100));
//匀速旋转*速度...需要一个较大的值
//target目标角度(四元数)
查看:MyComponent_RotationMachine虚拟旋转电机
查看:MyTool_Static.EulerAngleRotation欧拉角旋转方法
transform.LookAt(player1.position);//注视目标位置
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Vector3 dir = (ray.origin+ray.direction*100)-gameobject.transform.position;
//获取相机射线的欧拉角向量( gameobject 炮塔注视射线的角度)
//不明:原点+方向=真实方向位置(目标的世界坐标)
//目标的世界坐标 - gameobject炮塔的世界坐标 = 炮塔注视目标的向量
Quaternion target = Quaternion.LookRotation(dir);//计算得出四元数角度
gameobject.MoveRotation(Quaternion.RotateTowards(gameobject.rotation,target,Time.deltaTime*25));
//炮塔跟随转向到目标向量
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Quaternion target = Quaternion.LookRotation( new Vector3 (h,0,v));
//获取键盘向量角度(wsad)
transform.rotation =Quaternion.RotateTowards(transform.rotation,target,Time.deltaTime*rotateSpeed*100);
//匀速旋转*速度...需要一个较大的值
rigidbody001.angularVelocity = transform.up*h*angularSpeed;
//刚体.角速度矢量=变换.物体的绿色轴*方向*刚体的角速度矢量
//刚体的角速度向量。
transform.Rotate(new Vector3(0,0,-speed * Time.deltaTime));//坐标旋转
transform.Rotate(Vector3.up * Time.deltaTime,Space.Self);
//Space.Self局部坐标
//Space.World世界坐标
//更具角度设置旋转,不是很稳
transform.Rotate(transform.up*Mathf.Clamp(longitude/(smoothness*180),-1,1)*RotatingSpeed*Time.deltaTime,Space.World);
//(旋转轴+数学限制(我与目标的+-角度差/(平滑度*180),-1,1)*旋转速度*时间比,世界坐标系)
查看:MyTool_Static.EulerAngleConversion欧拉角,角度转换计算
///
/// 自轴旋转欧拉角计算
///
/// 当前物体欧拉角(transform.eulerAngles)
/// 旋转轴向量及角度大小(Vector3.up)
/// return : 自转后的欧拉角
static public Vector3 AxisRotation_EulerAngles(Vector3 OriginEulerAngle,Vector3 direction){
return (Quaternion.Euler(OriginEulerAngle) * Quaternion.Euler(direction)).eulerAngles;
}
target为中心,以Vector3.up为轴进行旋转,类似于围绕某一点公转。
transform.RotateAround(target.position, Vector3.up, 20 * Time.deltaTime);
使物体绕y轴旋转,z轴一直指向target
transform.LookAt(player1.position);//注视目标位置
transform.rotation = Quaternion.identity;
//该四元数,相当于“无旋转”:这个物体完全对齐于世界或父轴。
transform.localEulerAngles = Vector3.zero;
//归0
查看:MyComponent_Vector3Constraint向量\欧拉角约束器
查看:MyTool_Static.EulerAngleConversion欧拉角,角度转换计算