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 unity_En9Jz88Uh8XLKA · Jan 11, 2021 at 02:14 PM · 2d-platformerpickup

How to pick up an Item in 2D

so i made a script where when i press "e" the item gets destroyed, but now i don't know how to get it into the players Hands.

 private bool pickUpAllowed;

 // Use this for initialization
 private void Start () {
 }
 
 // Update is called once per frame
 private void Update () {
     if (pickUpAllowed && Input.GetKeyDown(KeyCode.E))
         PickUp();
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.name.Equals("Girl"))
     {
         pickUpAllowed = true;
     }        
 }
 
 private void OnTriggerExit2D(Collider2D collision)
 {
     if (collision.gameObject.name.Equals("Girl"))
     {
         pickUpAllowed = false;
     }
 }

 private void PickUp()
 {
     Destroy(gameObject);
 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by andycodes · Jan 11, 2021 at 05:27 PM

There are a few ways you could tackle this:

  • Instead of deleting the object, you could instead make the player's hand the parent of the object you want to pick up, and set the local position and rotation appropriately to look nice in the players hands. You might also want to disable the collision or trigger depending on your implementation.

  • Create a dummy object that always exists as a child of the player's hand, and when you pick something up, just place the sprite of the pickup object as the sprite of the object in the player's hands. This could probably be refined to account for object scale.

The second method might not be as viable depending on what you plan to do with the object: if the object is, say, a sword, you'd want information about the sword like the attack speed, or how much damage it inflicts. In this situation, the first approach might be preferable.

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
avatar image
0

Answer by logicandchaos · Jan 11, 2021 at 07:25 PM

You can parent it to the player and set it on the correct localePosition, then it will move with your player, if that is what you want, to put it down you would have to unparent or destroy it.

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
avatar image
0

Answer by unity_En9Jz88Uh8XLKA · Jan 12, 2021 at 08:41 AM

Thanks to both of you for your fast answer! But I am a total noob and I tried to do this. But it doesn't work. I don't know how to write that code. Sorry for asking this but how would that code look like.

Comment
Add comment · Show 1 · 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 Llama_w_2Ls · Jan 12, 2021 at 09:36 AM 1
Share

You can parent it to the player and set it on the correct localePosition

This can be done like:

 PickedUpObject.transform.SetParent(Player.transform);
 PickedUpObject.transform.position = HandPosition;


when you pick something up, just place the sprite of the pickup object as the sprite of the object in the player's hands.

Can be done by changing the sprite on the sprite renderer:

 PlayerHand.GetComponent<SpriteRenderer>().sprite = PickedUpObject.GetComponent<SpriteRenderer>().sprite;


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

124 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 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 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 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 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 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 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

Help. Newbie trying to pick up, put down, and throw two kinds of boxes in a 2D space. Thought it would be easy 1 Answer

Lerpz 2D Tutorial - Can't Save Scene And Proceed 0 Answers

2D Platformer Level Design 0 Answers

How do I edit the first person controller to work with android? 0 Answers

2d Game with 3d Objects 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