- Home /
How do I make and object's position and rotation independent again?
Just as the title says. How can I make the position of an object independent again after making it dependent earlier. This is what I have right now:
if(ItemPickedUp == true){
// If you press the E key.
if(Input.GetKeyDown("e")){
// The CarryableObject becomes a child of the Utility Props again.
CarryableObject.transform.parent = UtilityProps.transform;
// The drop position.
CarryableObject.transform.position = new Vector3(Character.transform.position.x, Character.transform.position.y, Character.transform.position.z + OffsetDropPosition);
// The rotation is no longer relative to the Character.
CarryableObject.transform.rotation = Character.transform.rotation;
// The gravity gets reenabled.
CarryableObject.GetComponent<Rigidbody>().useGravity = true;
// The child collider if-statements.
if(Child1 != null){
Child1.GetComponent<Collider>().enabled = true;
}
if(Child2 != null){
Child2.GetComponent<Collider>().enabled = true;
}
if(Child3 != null){
Child3.GetComponent<Collider>().enabled = true;
}
if(Child4 != null){
Child4.GetComponent<Collider>().enabled = true;
}
}
}
While an object has a parent, its relative position and rotation are transform.localPosition
and transform.localRotation
Obviously once they have no parent, it is back to transform.position
and transform.rotation
To unparent an object you should simply say
ChildX.transform.SetParent(null);
If this was not helpful, then you need to define what your problem is better.
Answer by ElCodeMonkey · May 13, 2017 at 09:28 AM
I believe you can just set the parent to null
Your answer
Follow this Question
Related Questions
Get closest Vector3 position from a GameObject and two Transforms (and the line inbetween them) 2 Answers
Multiple Cars not working 1 Answer
Help change Score text positioning using vector3 [C#] 1 Answer
Object not centering with screen.width/2 0 Answers
VUFORIA MULTI-TARGETS: SCRIPTING & CHILD HOLOGRAM VISUALIZATION 0 Answers