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 omarrob · Jun 24, 2012 at 10:38 AM · objectcharacterpickupboxdrop

Picking up a box

Hello i need my character to pick up a box when i press say "b" and drop it when i press b again, i would really appreciate it if someone can give me an idea how to do that, it is 2D and i use C#
Thank you

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

2 Replies

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

Answer by Berenger · Jun 24, 2012 at 02:30 PM

Usually there is several unknown objects you can pickup, so the distance is a good way to know which one is going to be picked up. You can use Physics.OverlapSphere for that (the pickup needs a collider). Follow those steps :

  • The player press the pickup button.

  • You need to know what pickup is around => Physics.OverlapSphere. Those pickups must have a collider, and it would help if they had a specific layer to use with the function.

  • This returns an array. Initialiaze a float to Mathf.Infity, go through the array and check the sqrMagnitude of (player.position - pickup.position). The lowest is the closest pickup, the one we are interested in, I'll call it P.

  • Set P child of the player, reset localPosition and localRotation, call P.gameObject.SetAciveRecursively(false).

  • In your player script, declare an ArrayList and call it bag. Then, after the last point, bag.Add(P).

  • If the player press the drop button, get the first or last element of the arraylist (best case scenario, there is a GUI to choose which object to drop, but let's keep it simple) then remove it from the list. Activate it, unparent it and done !

If you have a problem, tell me at which point.


Here is a code to pick up the box and hold it in front of you. You'll need an animation to have the hands at the right place though. This is untested and might not compile.

 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     // An object need to closer than that distance to be picked up.
     public float pickUpDist = 1f;
     
     private Transform carriedObject = null;
     private int pickupLayer = 1 << LayerMask.NameToLayer( "Pickup" );
 
     private void Update()
     {
         if( Input.GetButton( "pickup" ) ) // Define it in the input manager
         {
             if( carriedObject != null ) // We're holding something already, we drop
                 Drop();
             else // Nothing in hand, we check if something is around and pick it up.
                 PickUp();
         }
     }
 
     private void Drop()
     {
         carriedObject.parent = null; // Unparenting
         carriedObject.gameObject.AddComponent( typeof(Rigidbody) ); // Gravity and co
         carriedObject = null; // Hands are free again
     }
 
     private void PickUp()
     {
         // Collect every pickups around. Make sure they have a collider and the layer Pickup
         Collider[] pickups = Physics.OverlapSphere( transform.position, pickUpDist, pickupLayer );
 
         // Find the closest
         float dist = Mathf.Infity;
         for( int i = 0; i < pickups.Length; i++ )
         {
             float newDist = (transform.position - pickups[i].transform.position).sqrMagnitude;
             if( newDist  < dist )
             {
                 carriedObject = pickups[i].transform;
                 dist = newDist;
             }
         }
     
         if( carriedObject != null ) // Check if we found something
         {
             // Set the box in front of character
             Destroy( carriedObject.rigidbody );
             carriedObject.parent = transform;
             carriedObject.localPosition = new Vector3( 0, 1f, 1f ); // Might need to change that 
         }
     }
 }
Comment
Add comment · Show 9 · 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 omarrob · Jun 24, 2012 at 03:16 PM 0
Share

i will try my best and let you know .. thank you for answering.

avatar image omarrob · Jun 25, 2012 at 10:52 AM 0
Share

I tried to follow this but i guess too little experience on my side, i ended up lost >.<

avatar image Berenger · Jun 25, 2012 at 02:46 PM 0
Share

I'll edit my answer, try to make it clearer.

avatar image omarrob · Jun 25, 2012 at 08:04 PM 0
Share

okay, this is clear now but going back to my original question, i think you miss understood it, i need the player to pick up a box and move around while it is in his hands not in a bag.. i appreciate your effort in helping me

avatar image Berenger · Jun 25, 2012 at 08:12 PM 0
Share

Oh yep, I did misunderstand ! The idea is pretty much the same though, but you don't need a list as you can carry only one item at a time (I guess). And don't deactivate it, just remove the rigidbody if there is one. The tricky part will be the position between the hands. Is there a specific animation ?

Show more comments
avatar image
0

Answer by sameer · Jun 24, 2012 at 11:45 AM

if u want to take it then delete then like hp or somthing like that so

 bool deleted = true;
 if(Input.GetKey("b")){
 //delete the box Or Wear It
 deleted = true;
 
 }
 if(Input.GetKey("b")){
 if(deleted)
 {
 //DropIt Or UnWearIt
 }
 }
Comment
Add comment · Show 2 · 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 omarrob · Jun 24, 2012 at 01:38 PM 0
Share

Hello sameer, i don't get what this script is trying to do, i tested it and nothing happens, can you please expand on it ? thank you

avatar image Kalbytron · Nov 18, 2013 at 07:16 AM 0
Share

Hello, I'm trying to figure out why the offset is not working properly. As I set the z vector to 1, the object is suppose to be in front of the character, ins$$anonymous$$d it moves 1 forward on the world z-axis from the character is standing. Why is that?

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

8 People are following this question.

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

Related Questions

Picking up objects 1 Answer

Item pickup and drop function 2 Answers

changing the 3D character or character's body after picking up an object 0 Answers

How to pick / drop object 1 Answer

Can someone help me with the initialize and pick up script, please? 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