first commit
This commit is contained in:
49
Assets/dobot/Sc/RobotJoint.cs
Normal file
49
Assets/dobot/Sc/RobotJoint.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user