- Home /
Rotate object related to Vector3.Distance
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
Answer by tormentoarmagedoom · Jul 04, 2018 at 05:01 PM
Hey.
I dont get exatcly what you need. Where needs to look at the child? At the parent? Or have the same rotation as the parent? Some draw will be nice to understand it!
But anyway, ypu know about LookAt() function? I think is what ypu need. Go read the api or look some tutorial to learn how to use it!
Byee!
Hi, I've investigated LookAt() before, but after some code and tweaks it isn't behaving the way I like. The idea is to have the object moving like this(I recorded a Gif in 3Ds$$anonymous$$ax animating the movement) but in both X and Z positions, forgibing the Y one. https://imgur.com/lH7voGO
Hinge joint doesn't work either.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Progress bar for player progress is backwards 1 Answer
Changing distance with Vector3.Distance 1 Answer
Camera movement 1 Answer
change properties of gameObject 1 Answer