Решил я сделать контроллер меха
и написал вот такой код для поворота его частей, всё работает как часы, вот только не знаю насколько сильно этот код не оптимизирован или может есть лучшее решение данных действий, подскажите кто знает. За ранее благодарен всем!
Скрытый текст:
public PartInfo[] mechParts = new PartInfo[5];
void FixedUpdate () {
foreach (PartInfo part in mechParts)
{
if (part.canRotate == true && part.mechPart != null)
{
RotateParts(part.mechPart, part.rotSpeed, part.can_RotateX, part.limX, part.minX, part.maxX, part.angleX,
part.can_RotateY, part.limY, part.minY, part.maxY, part.angleY);
}
}
}
private void RotateParts(Transform part, float rotSpeed, bool canX, bool limX, float minX, float maxX, float angleX, bool canY, bool limY, float minY, float maxY, float angleY)
{
Vector3 dir = aimPoint.position - part.transform.position;
// Create new rotation towards the target
Quaternion TargetRotation = Quaternion.LookRotation(dir, Vector3.up);
Quaternion NewRotation = Quaternion.Slerp(part.rotation, TargetRotation, rotSpeed * Time.fixedDeltaTime);
// Set the new rotation of the base.
part.rotation = NewRotation;
if (canX == true)
{
//Clamp rotation
if (limX == true)
{
angleX = Mathf.Clamp(ClampAngle(part.localEulerAngles.x), maxX, minX);
angleX = RoundFloatToDecimal(angleX, 2);
}
else
{
angleX = RoundFloatToDecimal(part.localEulerAngles.x, 2);
}
}
else
{
angleX = 0;
}
if(canY == true)
{
if (limY == true)
{
angleY = Mathf.Clamp(ClampAngle(part.localEulerAngles.y), maxY, minY);
angleY = RoundFloatToDecimal(angleY, 2);
}
else
{
angleY = RoundFloatToDecimal(part.localEulerAngles.y, 2);
}
}
else
{
angleY = 0;
}
// Rotate
part.localEulerAngles = new Vector3(angleX, angleY, 0f);
}
[System.Serializable]
public class PartInfo
{
public string name;
public Transform mechPart;
[Header("Rotation Speed")]
public float rotSpeed = 5f;
public float returToIdleSpeedRot = 20f;
[Header("Rotation Limits")]
public bool canRotate;
public bool can_RotateX; // if is true than enable limits X
public bool limX;
public bool can_RotateY; // if is true than enable limits Y
public bool limY;
//vertical roratition
[Range(10.0f, 90.0f)]
public float minX = 60f;
[Range(-10.0f, -90.0f)]
public float maxX = -60f;
public float angleX;
//horizontal rorattion
[Range(0.0f, 20.0f)]
public float minY = 4f;
[Range(0.0f, -20.0f)]
public float maxY = -4f;
public float angleY;
}
void FixedUpdate () {
foreach (PartInfo part in mechParts)
{
if (part.canRotate == true && part.mechPart != null)
{
RotateParts(part.mechPart, part.rotSpeed, part.can_RotateX, part.limX, part.minX, part.maxX, part.angleX,
part.can_RotateY, part.limY, part.minY, part.maxY, part.angleY);
}
}
}
private void RotateParts(Transform part, float rotSpeed, bool canX, bool limX, float minX, float maxX, float angleX, bool canY, bool limY, float minY, float maxY, float angleY)
{
Vector3 dir = aimPoint.position - part.transform.position;
// Create new rotation towards the target
Quaternion TargetRotation = Quaternion.LookRotation(dir, Vector3.up);
Quaternion NewRotation = Quaternion.Slerp(part.rotation, TargetRotation, rotSpeed * Time.fixedDeltaTime);
// Set the new rotation of the base.
part.rotation = NewRotation;
if (canX == true)
{
//Clamp rotation
if (limX == true)
{
angleX = Mathf.Clamp(ClampAngle(part.localEulerAngles.x), maxX, minX);
angleX = RoundFloatToDecimal(angleX, 2);
}
else
{
angleX = RoundFloatToDecimal(part.localEulerAngles.x, 2);
}
}
else
{
angleX = 0;
}
if(canY == true)
{
if (limY == true)
{
angleY = Mathf.Clamp(ClampAngle(part.localEulerAngles.y), maxY, minY);
angleY = RoundFloatToDecimal(angleY, 2);
}
else
{
angleY = RoundFloatToDecimal(part.localEulerAngles.y, 2);
}
}
else
{
angleY = 0;
}
// Rotate
part.localEulerAngles = new Vector3(angleX, angleY, 0f);
}
[System.Serializable]
public class PartInfo
{
public string name;
public Transform mechPart;
[Header("Rotation Speed")]
public float rotSpeed = 5f;
public float returToIdleSpeedRot = 20f;
[Header("Rotation Limits")]
public bool canRotate;
public bool can_RotateX; // if is true than enable limits X
public bool limX;
public bool can_RotateY; // if is true than enable limits Y
public bool limY;
//vertical roratition
[Range(10.0f, 90.0f)]
public float minX = 60f;
[Range(-10.0f, -90.0f)]
public float maxX = -60f;
public float angleX;
//horizontal rorattion
[Range(0.0f, 20.0f)]
public float minY = 4f;
[Range(0.0f, -20.0f)]
public float maxY = -4f;
public float angleY;
}