Rotating Parent to get closest possible position of a child to a third object.

Hello and thanks for reading my post!

I have a parent, a child and a third object (See image). The parent has to be rotated in a way so that the child is as close as possible to the third object, without braking the hirarchy. The parent must not translate, only rotate. I need to accomplish this by scripting in one frame, without the use of physics.

I have tried to abstract my coding problem as far as possible to hopefully get a versatile solution for anyone in the community with similar problems. I understand I need the calculate the vectors and then apply a rotation, but it isn’t working properly so far.
I have tried to get a solution using quaternions for a couple of days now, but my coding skills simply aren’t sufficient.:frowning:

Thanks in advance for your help!

Jens

So, your parent, or some type of apmanager, must know
A) the postion of the parent, anchor point.
B) the random position of the third object.
C) the relative position of the child to the parent, anchor.

If you consider the positions of the parent to the child as a vector, a direction, like an arm of a clock, you are one step closer to solving.

You will then need to know the vector, or angle between the parent, anchor and the third object.

Then, the rotation value, if you have two angles (angle of parent child, angle of parent third) the angle you need to rotate by will be the difference of these two angles.

You can get angles via Angle()

using UnityEngine;

public class Foo : MonoBehaviour
{
    public Transform target;

    private Quaternion _childRPos;

	void Start ()
	{
	    _childRPos = Quaternion.FromToRotation(transform.forward, transform.GetChild(0).position-transform.position);
	}
	
	void Update ()
	{
        transform.rotation = Quaternion.LookRotation(transform.position - target.position) * _childRPos; 
	}
}

upd
No Matter seems its works in only for one specific child position.

upd
At this time it’s work properly :mrgreen:

using UnityEngine;

public class Foo : MonoBehaviour
{
    public Transform target;

    private Quaternion _childRPos;

	void Start ()
	{
        _childRPos = Quaternion.FromToRotation(transform.GetChild(0).localPosition, Vector3.forward);
	}
	
	void Update ()
	{
        transform.rotation = Quaternion.LookRotation(target.position - transform.position) * _childRPos; 
	}
}

…indeed, it does! like a charm!:slight_smile:

Thank you so much mouurusai, I’m very impressed!
Hopefully this script will be useful to others as well.

BEST

Jens

and also many thanks to you, renman3000!
I had your thoughs, but couldn’t get it working…

#pragma strict

var target : Transform;
private var _childRPos : Quaternion;
    
function Start () {

_childRPos = Quaternion.FromToRotation(transform.GetChild(0).localPosition, Vector3.forward);

}

function Update () {
transform.rotation = Quaternion.LookRotation(target.position - transform.position) * _childRPos;
}

here’s the JS-version of it.