- Home /
Behave like another object is parent?
Short and simple quastion: Gameobject A should behave like it is the child of gameobject B, even if it's not really the child. That means it should set its rotation and position like its gameobject B's child. Is it possible to do that with a script?
Answer by Peter G · Feb 24, 2011 at 08:50 PM
If you are using a physics controlled object, then you should use a fixed joint because it will do all the managing for you and you can set it up to detach, otherwise you could use a script.
Hinge Joints are your best choice, but I'll explain this script if you need it. It saves the distance between the child and the parent. Then it creates a new Vector3 that stores that position rotated by the parent's rotation. The child's position is the new offset + the parent's position.
How the loop works. Call ActivateParent()
to activate or deactivate the loop. It sets isParented
to flip, and if it flips from false to true, it calls the coroutine ParentObject described above. The coroutine then runs until isParented
becomes false which is when you call ActivateParent()
again. This way you don't have any unnecessary Update calls just checking to see if the boolean is true. It starts when you call ActivateParent()
and ends when you call it again. Simple enough right.
var parent : Transform; private var offset : Vector3; private var isParented = false;
function ActivateParent () { offset = transform.position - parent.position; //offset is the difference between the 2 pos's isParented = !isParented; if(isParented) ParentObject(); }
function ParentObject () {
while(isParented) {
var curOffset = offset * parent.rotation;
//this will rotate the offset position by the parent's rotation
transform.position = parent.position + curOffset;
//move this object
transform.rotation = parent.rotation;
//This line is overly simplified. You really need to account for the objects rotation
//at the beginning.
yield;
}
}
Answer by Alec-Slayden · Feb 24, 2011 at 08:23 PM
you can either actually parent it to the object in a script like so:
GameobjectA.transform.parent = GameobjectB.transform;
and detach when you wish like so:
GameobjectA.transform.parent = null;
If you need B to have no children, you can create an empty game object at its position, set to follow it, and set that as the parent to A.
If you need A to have no parent (or a specifically different one), then the only way I can think of is to calculate the change to B's transform based on when A started to pretend to be a child. Here's a sample javascript for adjusting position. For rotation some fiddling will need to be done with Rotate Around I believe.
var fakeParent : Transform;
private var parentOriginP : Vector3; private var isPretending = false; private var childOriginP : Vector3;
function Update(){
if (isPretending){
var positionChange = fakeParent.position - parentOriginP;
transform.position = childOriginP + positionChange;
}
}
function StartPretending(){ //call this when you want to pseudoparent. childOriginP = transform.position; parentOriginP = fakeParent.position; isPretending = true; }
Overall I believe it'll be much easier to manage if you can get away with transform.parent on the fly.
EDIT: fixed code problem, previously would have compounded the 'child' position to keep moving
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Camera follow ball along cylinder 1 Answer
Where to find original unity Parent/Child script? 2 Answers
How can I identify a non-uniform scaled mesh, and fix it? 1 Answer