注:该组件用于旋转位置模拟计算,调用其方法返回一个欧拉角,不会直接作用到游戏物体,可嵌入约束器
查看:MyComponent_Vector3Constraint向量\欧拉角约束器
查看:MyTool_Static.EulerAngleRotation欧拉角旋转方法
查看:MyTool_Static.EulerAngleConversion欧拉角,角度转换计算
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* 作者:闪电Y黑客
*
* 日期:2019.3.25
*
* 功能:用于计算旋转角度的组件
*
* 版本:002
*
*/
/// <summary>
/// 虚拟旋转电机
/// </summary>
public class MyComponent_RotationMachine : MonoBehaviour
{
//===[公有]======
/// <summary>
/// 电机激活
/// </summary>
public bool MachineActivation = true;
/// <summary>
/// 电机运行速度(模式不同,速度也不一样)
/// </summary>
public float RotationMoveSpeed = 10;
/// <summary>
/// 电机旋转约束器
/// </summary>
public MyComponent_Vector3Constraint RotationConstraint;//向量\欧拉角约束器
//===[私有]======
private Vector3 RotationTarget;//目标角度
private Vector3 RotationSave;//电机旋转角度存档
private Quaternion RotationSave_Q;//四元数旋转目标
private Quaternion RotationTarget_Q;//四元数旋转存档
private float yVelocity;//SmoothDamp返回的当前速度 (暂时无用)
#region 取值
/// <summary>
/// 获取旋转电机角度
/// </summary>
/// <returns>return : 电机旋转角度</returns>
public Vector3 Get_RotationSave()
{
return RotationSave;
}
/// <summary>
/// 获取旋转电机四元数角度
/// </summary>
/// <returns>return : 电机四元数旋转角度</returns>
public Quaternion Get_RotationSave_Q()
{
RotationSave_Q = Quaternion.Euler(RotationSave);
return RotationSave_Q;
}
/// <summary>
/// 判断完成旋转
/// </summary>
/// <returns>return : 完成返回true</returns>
public bool If_CompleteRotation()
{
return RotationTarget == RotationSave;
}
/// <summary>
/// 判断完成四元素旋转
/// </summary>
/// <returns>return : 完成返回true</returns>
public bool If_CompleteRotation_Q()
{
return RotationSave_Q == RotationTarget_Q;
}
#endregion
#region 赋值
/// <summary>
/// 旋转电机重置
/// </summary>
/// <param name="EulerAngle">重置角度</param>
public void Set_Machine_Reset(Vector3 EulerAngle)
{
RotationTarget = EulerAngle;
RotationSave = RotationTarget;
RotationTarget_Q = Quaternion.Euler(RotationSave);
RotationSave_Q = RotationTarget_Q;
}
/// <summary>
/// 电机设置:电机激活
/// </summary>
/// <param name="Activation">激活(true\false)</param>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Set_MachineActivation(bool Activation)
{
MachineActivation = Activation;
return this;
}
/// <summary>
/// 电机设置:电机运行速度
/// </summary>
/// <param name="Speed">速度值</param>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Set_MachineMoveSpeed(float Speed)
{
RotationMoveSpeed = Speed;
return this;
}
#endregion
#region 操控
/// <summary>
/// 电机设置:向量操控式(相对于父物体坐标系的轴旋转)
/// </summary>
/// <param name="ControlledVector3">操控向量</param>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine SetTarget_Controlled(Vector3 ControlledVector3)
{//控制旋转
if (MachineActivation)
{
RotationTarget = ControlledVector3 + RotationSave;
RotationTarget_Q = Quaternion.Euler(RotationTarget);
}
return this;
}
/// <summary>
/// 电机设置:三轴操控式(相对于父物体坐标系的轴旋转)
/// </summary>
/// <param name="x">x轴操控值</param>
/// <param name="y">y轴操控值</param>
/// <param name="z">z轴操控值</param>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine SetTarget_Controlled(float x, float y, float z)
{//控制旋转
if (MachineActivation)
{
RotationTarget = RotationSave + new Vector3(x, y, z);
RotationTarget_Q = Quaternion.Euler(RotationTarget);
}
return this;
}
/// <summary>
/// 电机设置:目标欧拉角旋转
/// </summary>
/// <param name="TargetEulerAngle">目标欧拉角</param>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine SetTarget_EulerAngle(Vector3 TargetEulerAngle)
{//目标欧拉角旋转
if (MachineActivation)
{
RotationTarget = TargetEulerAngle;
RotationTarget_Q = Quaternion.Euler(RotationTarget);
}
return this;
}
/// <summary>
/// 电机设置:目标向量(将向量转换为欧拉角)
/// </summary>
/// <param name="TargetVector3">目标向量</param>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine SetTarget_Vector3(Vector3 TargetVector3)
{//目标向量旋转
if (MachineActivation)
{
RotationTarget_Q = Quaternion.LookRotation(TargetVector3);//将向量转换为四元素
RotationTarget = RotationTarget_Q.eulerAngles;//四元素转换为欧拉角
}
return this;
}
#endregion
#region 电机运行
/// <summary>
/// 电机运行:欧拉角匀速旋转(速度越大越快,需要较大的值)
/// </summary>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Run_EulerAngle_MoveTowardsAngle()
{
RotationSave = MyTool.Static.EulerAngleRotation.MoveTowardsAngle(RotationSave, RotationTarget, RotationMoveSpeed * Time.deltaTime);
RotationSave_Q = Quaternion.Euler(RotationSave);
return this;
}
/// <summary>
/// 电机运行:欧拉角差值旋转(速度越大越快)
/// </summary>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Run_EulerAngle_LerpAngle()
{
RotationSave = MyTool.Static.EulerAngleRotation.LerpAngle(RotationSave, RotationTarget, RotationMoveSpeed * Time.deltaTime);
RotationSave_Q = Quaternion.Euler(RotationSave);
return this;
}
/// <summary>
/// 电机运行:欧拉角平滑旋转(速度越小越快)
/// </summary>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Run_EulerAngle_SmoothDampAngle()
{
RotationSave = MyTool.Static.EulerAngleRotation.SmoothDampAngle(RotationSave, RotationTarget, ref yVelocity, RotationMoveSpeed);
RotationSave_Q = Quaternion.Euler(RotationSave);
return this;
}
//===[四元数旋转]======
/// <summary>
/// 电机运行:四元数匀速旋转(速度越大越快,需要较大的值)
/// </summary>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Run_Quaternion_RotateTowards()
{
RotationSave_Q = Quaternion.RotateTowards(RotationSave_Q, RotationTarget_Q, RotationMoveSpeed * Time.deltaTime);
RotationSave = RotationSave_Q.eulerAngles;
return this;
}
/// <summary>
/// 电机运行:四元数差值旋转(速度越大越快)
/// </summary>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Run_Quaternion_Slerp()
{
RotationSave_Q = Quaternion.Slerp(RotationSave_Q, RotationTarget_Q, RotationMoveSpeed * Time.deltaTime);
RotationSave = RotationSave_Q.eulerAngles;
return this;
}
//===[自轴旋转]======
/// <summary>
/// 电机运行:欧拉角操控式自转(相对于自身坐标系的轴旋转)
/// </summary>
/// <param name="OriginEulerAngle">物体欧拉角(transform.eulerAngles)</param>
/// <param name="direction">方向(Vector3.up)</param>
/// <param name="Controlled">操控数(1/-1)操控正反转</param>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Run_ShaftControlled(Vector3 OriginEulerAngle, Vector3 direction, float Controlled)
{
if (MachineActivation)
{
RotationSave = MyTool.Static.EulerAngleConversion.AxisRotation_EulerAngles(OriginEulerAngle, direction * RotationMoveSpeed * Time.deltaTime * Controlled);
RotationSave_Q = Quaternion.Euler(RotationSave);
}
return this;
}
#endregion
#region 限制器
/// <summary>
/// 在父系坐标上限制旋转
/// </summary>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Constraint_Local()
{
if (RotationConstraint != null) RotationSave = RotationConstraint.Constraints_EulerAngle_PN180(RotationSave);//检测限制器,是否进行限制
return this;
}
/// <summary>
/// 在目标角度上限制旋转
/// </summary>
/// <returns>return : 电机组件</returns>
public MyComponent_RotationMachine Constraint_Scope()
{
if (RotationConstraint != null)//检测限制器,是否进行限制
{
Vector3 offsetRotation = RotationSave - RotationTarget;//偏移计算
offsetRotation = RotationConstraint.Constraints_Vector3(offsetRotation);//约束计算
RotationSave = offsetRotation + RotationTarget;//电机存档角度
}
return this;
}
#endregion
}
查看:MyComponent_Vector3Constraint向量\欧拉角约束器
使用例子:
public class ceshi_paota00 : MonoBehaviour {
public Transform Target;
public MyComponent_RotationMachine MyComponent_RotationMachine;
private Vector3 to_Target;
void Update()
{
Vector3 TargetlocalPosition = transform.parent.InverseTransformPoint(Target.position);
//目标局部坐标 = 本炮塔.父物体.世界坐标转局部坐标(目标世界坐标);
//作用:使得目标与本炮塔处于同一坐标系下;
to_Target = TargetlocalPosition- transform.localPosition;//获取目标向量
transform.localEulerAngles = MyComponent_RotationMachine
.SetTarget_Vector3(to_Target)//给电机设置目标向量
.Run_EulerAngle_MoveTowardsAngle()//运行恒速旋转模式
.Constraint_Local()//以父系坐标进行限制
.Get_RotationSave();//获取电机旋转后的角度存档
Debug.DrawRay(transform.position, transform.forward * 100, Color.yellow);//射线画
}
}
约束器约束x,z轴,嵌入电机,电机激活,速度100,ceshi_paota00 测试炮塔使用旋转电机
x轴被约束了,炮塔只能y轴旋转
解除x轴约束
炮塔X,Y轴都能旋转,并指向目标
限制测试:
x轴最大限制0,最小限制-60(俯角为0度,仰角60度)
y轴最大限制45,最小限制-90(右转45度,左转90度)
z轴都为0,也就是不可旋转