Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 replay11 · May 26, 2011 at 05:32 PM · rigidbodytriggerparentipadpickup

Touch the same GUITexture button to Pick Up and Drop a Rigidbody Game Object

Ok I have been struggling for days now trying to figure out how to code in javascript to get a mechanical arm in my game to pick up a rigidbody and then drop it whenever you touch a guitexture button on the iPad screen. I have searched through the forum and UnityAnswers and the web, but I can't seem to find an example for this. Everything that comes up in my search results is geared towards drag and drop with a mouse click rather then touch a GUITexture button to parent a rigidbody and touch again to unparent.

My goal is to use the same GUITexture button on screen... touch the button once and the mechanical arm animates down, picks up the rigidbody and then animates back up. Touch the same GUITexture button again and then the mechanical arm animates down again but this time drops the rigidbody. I would also like the mechanical arm to be invisible before you touch the GUITexture button so it "magically" appears and animates down after you touch the button. So far I have been able to pick up a rigidbody, but I can't seem to drop it. I also only want to be "holding" one rigidbody object at a time. The objects in my scene that I want to pickup I have given them a tag called "pickup". I have attached my script and a sphere collider object to a child of my mechanical arm game object (ie. to the "hand" of the mechanical arm)... I have pasted my script below. Please tell me if there is something I'm overlooking.


 var mechanicalArm: GameObject;
 var LaunchMechArmButton : GUITexture;
 var itemsHolding: int;
 var buttonPressCount: int;
 var mechArmVisible: boolean;
 
 
 
 // make the mechanical arm invisible by deactivating it's game object along with all of it's children
 //mechanicalArm.SetActiveRecursively(false);
 
 function Update () 
 {
 
 // begin "for" loop to check for iPhone touches    and create a variable "evt" to store them in
    for (var evt : Touch in Input.touches) 
    { 
        
     // hit tests: create variables to store hit test results then test for hit inside each GUITexture
     var LaunchMechArmButtonHitTest = LaunchMechArmButton.HitTest(Input.mousePosition);
         
         // first check to for which touch phase (Began, Stationary, Ended)
           if (evt.phase ==TouchPhase.Began) 
          { 
              
              // nested if statements that tell what to do if each hit test is true 
              
                 if(LaunchMechArmButtonHitTest) 
                {
                        // make mechanical arm visible by activating it's game object and all it's children
                     //mechanicalArm.SetActiveRecursively(true);
                        // play sound effect for mechanical arm
                        audio.Play();
                     // store a boolean value for whether or not the mechanical arm is visible
                     mechArmVisible = true;
                     // play the animation of the mechanical arm when button is touched        
                      mechanicalArm.animation.Play("Take 001");
                      // keep track of how many times this button has been pressed or touched
                     buttonPressCount++;    
                    }
                    
  
          }   
 
    }        
 }
 
 
 
 function OnTriggerEnter (other : Collider) 
 {
     
                 // Get the transform of the pickup
                 var collisionTransform = other.gameObject.transform; 
 
                         //Only pick up a game object with a tag of "pickup"    
                         if (other.gameObject.tag == "pickup")
                         {
                             if(itemsHolding < 1)
                             // make it a child of the current object
                             collisionTransform.parent = transform;
                             // Don't let the rigidbody be affected by physics!
                             collisionTransform.rigidbody.isKinematic = true;
                             // Turn Gravity OFF so rigidbody doesn't fall through the floor
                             collisionTransform.rigidbody.useGravity = false;
                             // make it point towards the object that picked it up
                             // place it behind the current object
                             //collisionTransform.localPosition = -Vector3.forward * 5;
                             collisionTransform.LookAt(transform);                             
                             // Keep track of how many items you are holding
                             itemsHolding++;
 
 
                         }
 
     
 }
 
 // item management
 
 function OnTriggerStay (other : Collider) 
 {
     var collisionTransform = other.gameObject.transform;
     
                                         if (itemsHolding > 0 && buttonPressCount > 1 && mechArmVisible)
                                         {
                                                 collisionTransform.parent = null;
                                                 itemsHolding--;
 
 
                                                 audio.Play();
                                                 mechanicalArm.animation.Play("Take 001");
                                                 yield WaitForSeconds (2);
                                                 collisionTransform.parent = null;
                                                 collisionTransform.rigidbody.isKinematic = false;
                                                 collisionTransform.rigidbody.useGravity = true;
                                                 collisionTransform = null; 
                                         }
     
      
     
 }

Comment
Add comment · Show 3
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 almo · May 26, 2011 at 06:00 PM 0
Share

You should ask your question once and wait, or edit the old one if you made a mistake.

avatar image replay11 · May 27, 2011 at 08:36 PM 0
Share

I couldn't figure out how to remove the first one.... Which gave me an error when I submitted it... So I didn't even know there were two versions until after I created the second question! I just figured out how to delete the first question! Thanks for your help, smart a?!$

(just now)joeyjr

avatar image Muzz5 · May 30, 2011 at 11:08 AM 0
Share

Out of interest, how much rep do you need to retag questions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Unamine · May 30, 2011 at 10:49 AM

Simply use an if so when and when not with the object

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Picking up items with Rigidbodies 2 Answers

Trigger BoxCollider Fails to Move With GameObject After Resize 0 Answers

How can I access a collider's parent's rigidbody? 1 Answer

How do I make my PickUp system for my guns have a smooth animation when you pick it up? (PickUp System script below) 0 Answers

Box collider triggers interfering with rigidbody movement 3 Answers


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