I am trying to replicate a robot arm in Unity.
I was successfully able to apply pan tilt roll movement on the joints with a slider value on the inspector.
However, I also want that the segment lengths are editable on the inspector. My problem is, when I do, the segments also rescale its children. I want them to rescale independently of each other. Additionally, I want that while I rescale the Segments, the elements in the heirarchy should still be connected to each other.
Here’s my script attached to RR(2):
using System.Collections.Generic;
using UnityEngine;
public class RobotController : MonoBehaviour
{
public Transform[] joints;
[Range(-360, 360)]
public float azimuth;
public GameObject obj_azimuth;
[Range(-90, 90)]
public float elevation = 0f;
public GameObject obj_elevation;
[Range(-360, 360)]
public float pan = 0f;
[Range(-360, 360)]
public float tilt = 0f;
[Range(-360, 360)]
public float roll = 0f;
[Header("Segments")]
[Range(0.5f, 10f)]
public float lengthSegment1;
public GameObject Segment1Holder;
[Range(0.5f, 10f)]
public float lengthSegment2;
public GameObject Segment2Holder;
[Range(0.5f, 10f)]
public float lengthSegment3;
public GameObject Segment3Holder;
public GameObject RobotCamera;
void Start()
{
lengthSegment1 = 2f;
lengthSegment2 = 2f;
lengthSegment3 = 2f;
UpdateSegmentLengths();
}
void Update()
{
obj_azimuth.transform.eulerAngles = new Vector3(elevation, azimuth, 0);
joints[0].localRotation = Quaternion.Euler(0, pan, 0); // Pan (Rotate around Y-axis)
joints[1].localRotation = Quaternion.Euler(tilt, 0, 0); // Tilt (Rotate around X-axis)
joints[2].localRotation = Quaternion.Euler(0, 0, roll); // Roll (Rotate around Z-axis)
}
void UpdateSegmentLengths()
{
Segment1.transform.localScale = new Vector3(Segment1.transform.localScale.x, Segment1.transform.localScale.y, lengthSegment1);
Segment2.transform.localScale = new Vector3(Segment2.transform.localScale.x, Segment2.transform.localScale.y, lengthSegment2);
Segment3.transform.localScale = new Vector3(Segment3.transform.localScale.x, Segment3.transform.localScale.y, lengthSegment3);
}
}