• 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
Question by Suave · Jul 24, 2017 at 04:32 AM · 2d game2d-physics2d collision

OnTrigger and Input.getkey on picking up weapons from the ground.

Hey, I've been trying to make it so I have to click E in order to pick a weapon from the ground. I know that OnTrigger doesnt get called every frame so I tried creating a bool to make it work. However it didnt (other wise i wouldn't be here) and to me, the code makes sense. I'm sure I'm missing out on something, can you help me? Here's the code:

void OnTriggerStay2D(Collider2D other){

         if (other.CompareTag("Weapon"))
         {
             // OnWeaponPickup(other.transform);
             weapOnGround = other.transform;
             
             OnWeapTrig = true;
         }
         else
         {
             OnWeapTrig = false;
         }
     }

public void OnWeaponPickup()

{

         Transform weapon = weapOnGround;

         if(OnWeapTrig == true)
         {
             if (IsHolsterFull())
             {
                 DestroyWeaponInCurrentSlot();
             }
             else
             {
                 if (currentWeapon != -1)
                 {
                     DisableWeapon(currentWeapon);
                 }

                 currentWeapon++;
             }


             EquipWeaponInCurrentSlot(weapon);
         }
         
         
     }
Comment

People who like this

0 Show 7
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 ownerfate · Jul 24, 2017 at 12:29 PM 0
Share

try adding a rigid body, and ticking the gravity off, sometimes, you'll need to have these on triggers to make them work.

( even with the more common mouse over methods )

avatar image Suave ownerfate · Jul 24, 2017 at 03:39 PM 0
Share

I already had a 2drigidbody with gravity = 0 (since its a top down game)

avatar image tanoshimi · Jul 24, 2017 at 12:41 PM 0
Share

You don't need OnTriggerStay2D to repeatedly set a bool every frame - it would be more efficient to use OnTriggerEnter2D. Please post a screenshot showing your collider/rigidbody settings applied to each object.

avatar image Suave tanoshimi · Jul 24, 2017 at 03:39 PM 0
Share

I later changed it to Trigger Enter and it didnt work anyways

avatar image TreyH · Jul 24, 2017 at 01:17 PM 0
Share

Use The OnTriggerEnter / Exit. Additionally, you may pass over more than one weapon, or pass over a weapon immediately before a non-weapon.

In the latter case, your code would evaluate OnWeapTrig to false, despite you standing on a weapon.

You might have a few issues in the second statement, but without any extra explanations we can't help too much there:

 // Keep track of the weapons we're in range of
 private List<Transform> nearbyWeapons = new List<Transform> ();
 
 // This will be called whenever you enter a trigger collider
 void OnTriggerEnter2D (Collider2D other)
 {
     // Check if this was a weapon
     if ( other.CompareTag ("Weapon") )
     {
         // If so, add it to our list
         this.nearbyWeapons.Add (other);
     }
 }
 
 // This will be called whenever you enter a trigger collider
 void OnTriggerExit2D (Collider2D other)
 {
     // Check if this was a weapon
     if ( other.CompareTag ("Weapon") )
     {
         // Check if we had this previously, and if so remove it
         if ( this.nearbyWeapons.Contains (other) )
         {
             this.nearbyWeapons.Remove (other);
         }
     }
 }
 
 // Your pickup function
 public void OnWeaponPickup ()
 {
     // Make sure we were standing on something
     if ( this.nearbyWeapons.Count == 0 )
         return;
 
     // You can choose what to do here for determining which weapon to use
     Transform weapon = this.nearbyWeapons [0];
 
     // Possible issue?  If your weapon is full, it looks like you are
     // trying to remove the current one, but then this statement will 
     // do nothing else following that?
     //
     if ( IsHolsterFull () )
     {
         DestroyWeaponInCurrentSlot ();
     }
     else
     {
         if ( currentWeapon != -1 )
         {
             DisableWeapon (currentWeapon);
         }
         currentWeapon++;
     }
     EquipWeaponInCurrentSlot (weapon);
 }




avatar image Suave TreyH · Jul 24, 2017 at 03:40 PM 0
Share

Everything is working fine, I'm just having dificulties doing it so only if i press e will the OnWeaponPickUp get called

avatar image TreyH Suave · Jul 24, 2017 at 05:09 PM 0
Share

To capture input, you can use Update and the Input.GetKeyDown function:

 void Update ()
 {
     if ( Input.GetKeyDown (KeyCode.E) )
     {
         this.OnWeaponPickup ();
     }    
 }

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by simone9725 · Jul 24, 2017 at 05:12 PM

Why do you use on trigger stay ,the collision I guess happens one time and then destroy or do other stuff. You can change this:

void OnTriggerStay2D(Collider2D other) to void OnTriggerEnter2D(Collider2D other)

Comment

People who like this

0 Show 0 · 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

Answer by shadowpuppet · Jul 24, 2017 at 05:29 PM

I have several scripts that also use the E key to do stuff. i use onTriggerStay with Getkey because you only enter the trigger once and would need to be pressing the E key as you enter while you stay there and press E something happens.So the condition of being in trigger zone and pressing E is what this script does. The script goes on the triggerzone

         void OnTriggerStay (Collider other) {
             if(other.tag == "Player")
             {
             if (Input.GetKeyDown(KeyCode.E))
             {
 //something happens
 }
 }
 }
Comment

People who like this

0 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 TreyH · Jul 24, 2017 at 05:39 PM 0
Share

Unless the physics updates and UI updates are synced, the centermost If statement isn't going to execute reliably.

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

77 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

Related Questions

2D collision matrix does not work in unity 2020 4 Answers

Use Point Effector 2D w/ Tags 0 Answers

How to add collision to 2d animation 1 Answer

Tilemap Collider 2D not generating colliders...,Tilemap Colliders won't generate.... 0 Answers

How can I make a "mobile" boundary? 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