I’m trying to Instantiate an object based on the local position rotation of another object without parenting.
I made a crappy diagram to show what I mean.
Can anyone help please?
Thanks
I’m trying to Instantiate an object based on the local position rotation of another object without parenting.
I made a crappy diagram to show what I mean.
Can anyone help please?
Thanks
Yeah? You get the first object’s position and orientation, and you instantiate a new object based on that.
There’s even a method that let you pass a position/orientation at the moment of instantiation;
So, what have you tried? Where are you blocked?
It’s not the instantiating part that I’m having issues with, it’s the positioning, I’ve tried…
public GameObject targetObject;
public Vector3 offset;
void Start () {
transform.rotation = targetObject.transform.rotation;
transform.position = targetObject.transform.localPosition + offset;
}
and a bunch of different combinations of the above.
Why localPosition and not position? Is the target parented to something? Should the offset be dependent of the target’s orientation?
I’ve tried position too, I’ve tried positioning at the target object position, then rotation to match the target objects rotation then offsetting but it ends up moving in world space.
Should I just instantiate objects, parent them to the target object, position them relative to the target then unparent them?
Sounds a bit dodgy.
You can convert a position relative to a transform into world space using transform.TransformPoint.
Thanks, that did the trick:
public GameObject targetObject;
public Vector3 offset;
void Start () {
transform.rotation = targetObject.transform.rotation;
transform.position = targetObject.transform.position;
transform.position += transform.TransformPoint(offset);
}