- Home /
How to make a child object follow a target within the constrain of parent object
I want my child object to follow my target's position within the parent's constrain. My target moves only from the x position for now. I used Unity's function transform.LookAt, and it looks at the position of my target which is not what I'm trying to achieve. Please look at the image to get an idea. Any help is appreciated.
Answer by sacredgeometry · Nov 29, 2020 at 03:55 PM
You can do this a bunch of ways but I would first start by normalising your range of movement between a consistent range probably the most useful would. be 0.0 to 1.0.
That way you have a normalised range of input of your target which goes between 0-1 and you have a normalised range of input of your child objects that go from 0-1.
Explanation
The easiest way to normalise is just to use division and would work like this currentPosition/maxPosition=normalisedPosition
For example:
If our child objects total range of motion is 12
units and our child's position was only 3
units across.
It would mean we are essentially a quarter of the distance across its entire range of movement. So the normalised value would be a quarter. Or: 3 / 12 = 0.25
We could then use that value i.e. 0.25
with any other range.
So, say our targets total range is much larger at 450
.
Multiplying our child's normalised position value of 0.25
with the targets total range of 480
gives us 120
i.e 480 * 0.25 = 120
.
Meaning our absolute target position for the relative normalised child position would be 120
.
Does that make sense and is that what you were trying to achieve?
Clamping
After you have done that you could also clamp the normalised values between 0.0 and 1.0 to stop them moving outside of their bounds.
Reading
Answer by shaaafeee · Dec 06, 2020 at 07:59 AM
Yes that makes sense. But I'm not too good at maths. Can you elaborate it a bit?
Your answer
Follow this Question
Related Questions
Parenting an object changes its transform. 0 Answers
How to disable player gravity 2 Answers
Avoid scaling GameObject after parenting 2 Answers
Camera smoothing of a Child Camera 1 Answer
Make cube follow parent (player) on X & Z axis, but not Y? 2 Answers