I have a parent game object that is moved by player. This parent has a child that I want to follow the parent in position with a delay(already solved) and I need the child object to localRotate facing the movement of the parent, rotating towards X and Z only.
I made a Gif inside 3Ds Max with one axis(Hinge joint does the same in Unity, but I’m trying to make it in two Axis):
So far I have this:
void RotateAxis()
{
Vector3 trackDataX = new Vector3(track.transform.position.x, 0,0);
Vector3 trackDataZ = new Vector3(0,0,track.transform.position.z);
Vector3 childDataX = new Vector3(this.transform.position.x, 0,0);
Vector3 childDataZ = new Vector3(0,0, this.transform.position.z);
float distX = Vector3.Distance(trackDataX, childDataX);
float distZ = Vector3.Distance(trackDataZ, childDataZ);
this.transform.localRotation = new Quaternion(distZ, this.transform.localRotation.y, distX, this.transform.localRotation.w);
}
But the child object is not rotating towards the position direction of the parent.
This other code makes the children cube to rotate smoothly but doesn’t return its rotation to local 0,0,0 when the parent stops moving:
void LookAtTarget()
{
Vector3 targetPosition = new Vector3(trackLook.transform.localPosition.x, this.transform.localPosition.y, trackLook.transform.localPosition.z);
this.transform.localRotation = Quaternion.LookRotation(targetPosition, Vector3.up);
}
How can I achieve this?
Thanks in advance