Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alienjesus · Mar 07, 2011 at 11:10 PM · raycastingchildrenmovingdetach

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") &amp;&amp; (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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alienjesus · Mar 07, 2011 at 11:35 PM 0
Share

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.

avatar image by0log1c · Mar 07, 2011 at 11:59 PM 0
Share

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 :)

avatar image Alienjesus · Mar 08, 2011 at 12:11 AM 0
Share

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

avatar image by0log1c · Mar 08, 2011 at 12:45 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

(A* Pathfinding) Enemy is moving step by step when near player 3 Answers

How can I get the tag of a child object? 1 Answer

transform.DetachChildren() is deactivating my children's scripts after detaching from the parent 1 Answer

Add force to child 1 Answer

Children detach not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges