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 mcarthur03 · Sep 20, 2015 at 07:43 AM · equip

How to Equip and Dequip to hand in 2D

I'm making a top down shooter. I have a player. that player has a child in the position of their hand. this is where they will hold the object. how can I make it so if they press "e" and are in a range of the object, it is suddenly in the hand and only plays its script while it is in the hand? then if "e" is pressed again, how would I make the player drop it?

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
0
Best Answer

Answer by Ambrose998800 · Sep 26, 2015 at 05:52 PM

Try something like that:

 if(Item.GetComponent(ItemScript).ItemPlace == "HandRight"){
                     
                     if (HandRightUsed == false){
                     ItemInHandRight = Item.gameObject;
                     Item.gameObject.transform.position = HandRightEmpty.transform.position;
                     Item.gameObject.transform.parent = HandRightEmpty.transform;        
                     HandRightUsed = true;
                     HandRightEmpty.SendMessage("ItemIsEquipped", true, SendMessageOptions.DontRequireReceiver);
                     }
 
                 }

Then add the function ItemIsEquipped(); to the ItemScript that activates further functions.

For dropping: unparent the Item again, set by its rigidbody 'isKinematic = false' and give 'ItemRigidbody.AddForce(float);' to it in desired direction.

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 Xeong-Hu · Sep 20, 2015 at 11:54 AM

Hmm Well. I'm gonna guess this is a sidescroller 2d game and not a first person shooter. Haha I was totally confused at first. But it's fairly simple. When ever you press the E button just make the Weapon's Object's Position Equal to Hands Position.

So like. Script mode.

 public GameObject Weapon;
 public GameObject Hand;
 public bool NearWeapon;
 public bool Holding;
 
 if(KeyCode.E && NearWeapon == true){
  Holding = true;
 }

 if(KeyCode.E && Holding == true){
  Holding = false;
 }

   if (Holding == true)
  {
 Weapon.transform.position = Hand.transform.position;
  }

  if (Holding == false)
  {
 Weapon.transform.position = null;
   }
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 mcarthur03 · Sep 25, 2015 at 09:27 PM

Thanks a lot! (its a top down shooter, 2D) there are two more things, when I de-equip the item I want it to throw. which I could probably do with some adding of force. but I also want the item to NOT work when it is de-equipped. it there a way I could disable its code when it isn't equipped?

the current script also comes up with these errors, I guess you gave me code that works in unity four and I just have to alter it to work in unity 5, but I cant seem to figure out how. :(

Assets/Scripts/Hold.cs(35,42): error CS0037: Cannot convert null to UnityEngine.Vector3' because it is a value type Assets/Scripts/Hold.cs(24,29): error CS0019: Operator &&' cannot be applied to operands of type UnityEngine.KeyCode' and bool'

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I "equip" this weapon properly to a model? (IMG) 1 Answer

hold key unequip sword 0 Answers

Armour and armour changing 0 Answers

Picking up a flashlight 0 Answers

How to deal with different Weapon animations on Equip? 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