- Home /
Detach children on button press
I'm writing some code to enable you tomove object in the environment around using a tracked object with a camera. I have some code here which makes an object in front of my 'controller' become a child of the controller in order to freely move it around, and it works great. However, I also want to be able to drop the object back upon another button press, and I'm evidently doing something wrong. When I add this code, it makes the whole thing not work. I'm not sure why - maybe it's something to do with both pieces of code being in the Update function?
function Update () { var fwd = transform.TransformDirection (Vector3.forward); //Defining the travel direction of the raycast vector var hit : RaycastHit; //If Raycast collides with something, hit = true? var LayerMask = 1 << 9; //Interacts with 1 layer - Layer 9 - Moveable
var hasObject = false;
//Draws ray vector in Scene view Debug.DrawRay(transform.position, fwd * 50, Color.green);
//if Raycast finds a collider in front of this object in layer 9, print message 1, else print message 2. if (Physics.Raycast (transform.position, fwd, hit, 50, LayerMask)) { print ("There is something in front of the object!");
if (Input.GetButton ("Fire2") && (!hasObject)) { hasObject = true; var otherThing = hit.transform; otherThing.rigidbody.isKinematic = true;
}
otherThing.parent = transform;
}
**********************************************************************
if (Input.GetButton ("Fire2") && (hasObject)){
transform.DetachChildren();
otherThing.rigidbody.isKinematic = false;
hasObject = false;}
else print ("Nothing in front of the object!");
**********************************************************************
So the variable hasObject is meant to decipher whether an object has already been picked up by the controller or not. If an object is already attached, it's meant to detach it and make its rigidbody active again. If I disable the whole section of code between the lines of asterisks, the object parenting code works fine, but if its enabled, it doesn't. Anyone having any ideas what I'm doing wrong?
Answer by by0log1c · Mar 07, 2011 at 11:22 PM
The problem is having 2 separate if watching the same condition. Also I feel like the general order of things is somewhat confused. I'd go like this:
var hasObject:boolean; var otherThing:Transform;
if(Input.GetButton("Fire2")){ if(hasObject){ otherThing.rigidbody.isKinematic = false; otherThing.parent = null; hasObject = false; print("Dropped "+otherThing.gameObject.name+"\n"); }else{ if(Physics.Raycast(transform.position, fwd, hit, 50, LayerMask)){ otherThing = hit.transform; otherThing.rigidbody.isKinematic = true;
otherThing.parent = transform; hasObject = true; print("Picked up "+otherThing.gameObject.name+"\n"); }else{ print("Nothing to pickup!\n"); } } }
EDIT: Corrected, not tested. Assuming a single click to pickup and next click to release.
Thanks, this was me building up stuff over time and im quite new at this. 'll have a bit of a tidy up then and see how it goes.
I didn't mean to be rude, I usually try to write friendlier answer. Everyone gets better at making up programmation 'schema' with time. I had to make up the AI for an adventure game and I found out making a tree diagram can help visualize in times of need, just saying :)
Not a problem dude, I didn't perceive it as being rude. I ask these questions to become better, so all constructive feedback is appreciated. Anyway, after re-ordering the code in the manner you demonstrated, it now allows me to pick up and move the objects fine. However, once I've picked up an object, pressing the button still isn't letting me drop it again. I can't really see an issue with this one :S
mmm,after re-reading, and guessing the idea is that a simple click grab the object, then the next click release it, I realize I've made it subtly wrong, I'll edit.