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
1
Question by Cratuss96 · Aug 20, 2020 at 07:04 AM · 2dtopdownweaponchanging

Trying to Swap 2 Weapons

Hello,

I am relatively new to Unity and am currently going crazy because of a problem.

in a 2D topdown game I try to swap my current weapon with the weapon lying on the ground. Both have the "WeaponSystem" script with the following code for pickup/drop:

 private void Update()
     {
         MyInput();
     }
 
     private void MyInput()
     {
         if(Input.GetKeyDown(KeyCode.E) && findPlayerInRadius.inRange && !isActive)
         {
             GameObject activeWeapon = pivotpoint.transform.GetChild(0).gameObject;
             activeWeapon.GetComponent<GunSystem>().Drop();
             PickUp();
         } 
     }
 
     private void PickUp()   
     {
         isActive = true;
 
         //Attach Weapon to Player
         transform.parent = pivotpoint.transform;
         transform.localPosition = pos;
         transform.localRotation = Quaternion.identity;
         transform.localScale = new Vector3(1, 1, 1);
 
     }
 
     private void Drop()
     {
         isActive = false;
 
         //Unattach Weapon to Player
         transform.parent = null;
         transform.localRotation = Quaternion.identity;
         transform.localScale = new Vector3(1, 1, 1);
     }

According to the Visual Studio Debugger, the process now looks as follows: The MyInput() method is called for my object lying on the ground which has the effect that the active weapon is dropped and is therefore no longer "active". The weapon lying on the ground is then picked up and set "Active".

So far so good.

But then the problem occurs: the MyInput method is called again for the originally active weapon which is now lying on the ground. This has the effect of picking up the weapon and dropping the other one. In the game it looks like I don't pick up the other weapon at all, because it is dropped again immediately.

Unfortunately, I don't know how to get ahead at this point.

I hope you can help me^^

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by revolute · Aug 21, 2020 at 10:21 AM

The problem here is that you are taking input per object. On a single frame, if the item lying on the ground gets updated first and then the weapon you are holding gets updated next, then it will simply pick up the ground weapon first then immediately swap for the gun you just dropped. If you're going down this road, put a delay in picking up weapons.

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 Cratuss96 · Aug 21, 2020 at 12:57 PM 0
Share

ok i will try that. do you have any tips how i can do this? the first thing i see is the invoke method

avatar image Llama_w_2Ls Cratuss96 · Aug 21, 2020 at 04:52 PM 0
Share

Using an IEnumerator will allow you to delay a set of instructions by a specified amount of time. For example:

     IEnumerator SwitchWeapons()
     {
         yield return new WaitForSeconds(0.5f); //Delays for 0.5 seconds
         PickUpWeapon();
     }
 
     void PickUpWeapon()
     {
         //...
     }

$$anonymous$$ake sure that you have the using System tag at the top to get the IEnumerator class.

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

283 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 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 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 top-down multiple sprite rotation 1 Answer

Touch Buttons 0 Answers

Lines are disappearing and merging as I zoom out 2 Answers

2D Top Down Shooter Shooting Problems 1 Answer

8-directional orientation, top down 2D, seperate from movement 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