50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
[System.Serializable]
|
|
public class RobotJoint
|
|
{
|
|
public Transform JointTransform;
|
|
|
|
public enum RotationAxis { X, Y, Z }
|
|
public RotationAxis Axis = RotationAxis.Y;
|
|
|
|
public float MinLimit = -180f;
|
|
public float MaxLimit = 180f;
|
|
|
|
[HideInInspector] public float CurrentValue = 0f; // degrees
|
|
|
|
// Lưu offset ban đầu
|
|
private Quaternion baseRotation;
|
|
|
|
public void Init()
|
|
{
|
|
if (JointTransform != null)
|
|
baseRotation = JointTransform.localRotation;
|
|
else
|
|
Debug.LogWarning("JointTransform is not assigned in RobotJoint!");
|
|
}
|
|
|
|
public void SetValue(float angleDeg)
|
|
{
|
|
if (JointTransform == null) return;
|
|
|
|
CurrentValue = Mathf.Clamp(angleDeg, MinLimit, MaxLimit);
|
|
|
|
Vector3 axisVector = Vector3.zero;
|
|
switch (Axis)
|
|
{
|
|
case RotationAxis.X: axisVector = Vector3.right; break;
|
|
case RotationAxis.Y: axisVector = Vector3.up; break;
|
|
case RotationAxis.Z: axisVector = Vector3.forward; break;
|
|
}
|
|
|
|
// rotation = baseRotation * xoay quanh trục
|
|
JointTransform.localRotation = baseRotation * Quaternion.AngleAxis(CurrentValue, axisVector);
|
|
}
|
|
|
|
public float GetValue()
|
|
{
|
|
return CurrentValue;
|
|
}
|
|
}
|