• 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 CB-TV · Feb 05, 2014 at 08:53 PM · collisionrigidbodyvector3pick up

Pick Up Rigid Body

I have a script (I know it's a bit messy):

 function PickUpObject ()
 {    
      PickedUp = true;
      
     object.parent = Camera.main.transform; 
     
     rigidbody.useGravity = false;        
 }
 
 function Update ()
 {    
     if (PickedUp == true)
     {
         if (collisionCount == 0) //If collisionCount is 0
         {
             object.localPosition = Vector3.forward * 2; //Put objects local position to 2 * foraward.
         }
         
         if (Input.GetKeyUp ("e")) // If E is let go
         {
             PickedUp = false; //PickedUp is false
             
             object.parent = null; //Unparent object
             
             rigidbody.useGravity = true; //Enable object's gravity
             
             //rigidbody.isKinematic = false;
         }
     }
 }
 
 private var collisionCount = 0; //collisionCount is 0
 
 function OnCollisionEnter () //When object collides
 {
     collisionCount++; //+collisionCount
 }
 function OnCollisionExit () //When object stops colliding
 {
     collisionCount--; //-collisionCount
 }

This script doesn't work very well since the object glitches out when it collides with walls. Then I have this other script(It's not mine btw):

 function FixedUpdate() //Every fixed framerate frame
 {
    if(gravityGunState == GravityGunState.Free) //If  gGS = GGS is Free 
    {
      if(Input.GetButton("Fire1")) //If Fire1
      {
          
          var hit : RaycastHit; //Get info back
         if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)) //From the position, then forward, using hit, length, in a layer.
         {
             if(hit.rigidbody) //If it hits a rigidbody
             {
                 rigid = hit.rigidbody; ///Get the rigidbody of the object
                 gravityGunState = GravityGunState.Catch; //gGS = GGS is Catch
              }
         }
       }
     }
 
         else if(gravityGunState == GravityGunState.Catch) //Also, if not above, check if gGS = GGS is Catch
         {
             rigid.MovePosition(transform.position + transform.forward * holdDistance); //Moves rigid to this position + forward at holdDistance
             if(!Input.GetButton("Fire1")) //If fire1 is not on
                 gravityGunState = GravityGunState.Occupied; //gGS = GGS is Occupied
         }

This script seems a bit worse than the previous one since objects always go through colliders. (I know gravity isn't switched off and it reacts like a gravity gun with force(which is not what I want(I can easily change this later though))) Finally, I have this script that is in C#(It's also not mine):

 public float mCorrectionForce = 50.0f;
 
 public float mPointDistance = 3.0f;
 
  
 
 void FixedUpdate()
 
 {
 
     if(heldObject.contraints != RigidbodyConstraints.FreezeRotation)
 
         heldObject.contraints = RigidbodyConstraints.FreezeRotation;
 
     if(heldObject.useGravity)
 
         heldObject.useGravity = false;
 
  
 
     Vector3 targetPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0));
 
     targetPoint += Camera.main.transform.forward * mPointDistance;
 
     Vector3 force = targetPoint - heldObject.transform.position;
 
  
 
     heldObject.rigidbody.velocity = force.normalized * heldObject.rigidbody.velocity.magnitude;
 
     heldObject.rigidbody.AddForce(force * mCorrectionForce);
 
  
 
     heldObject.rigidbody.velocity *= Mathf.Min(1.0f, force.magnitude / 2);
 
 }

I do not understand this final script properly though. My question would be that what do you think is best and why. Also, if you can tell me basically what script 3 does then it would be great! I think script 3 works best but I haven't got to use it since to a simple compiler error I will fix. You don't have to give a detailed answer (unlike my question) but any reply would be greatly appreciated! (Sorry for the loooong question!)

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you pls help me fix this rigidbody collision problem 0 Answers

How I can make gameobject detect collisions and make every other gameobject be able to go through it? 1 Answer

OnCollisionEnter doesn't work if enemies and allies have rigidbody ? 1 Answer

Trouble with OnCollisionEnter and Exit 1 Answer

Rigidbody: how to get collisions for the current frame ? 0 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