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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kantus · Jan 28, 2015 at 09:48 AM · c#changeweaponswitchpersistence

Weapon Switching

Hi,

I'm making a game wherein the main character has a selection of different weapons, and can swap between them on the fly. Currently, the file structure for this is:

Player->Arm->Weapon->FirePoint

This setup allows Arm to rotate (via an attached script), Weapon rotating with it. FirePoint is, well, the place (Transform) where the bullets/crossbow bolts are spawned. Weapon is a GameObject with an attached Sprite Renderer and behaviour Script (so, "Musket.cs" in the case of the Musket weapon). Of note is that Musket.cs includes a MusketBall prefab, where MusketBall is another object with its own behaviour script, (MusketBall.cs). The other three weapons in the game have similar structures (so you could read Crossbow/CrossbowBolt instead of Musket/Musketball).

The problem is this: I want it to be such that, when the user presses a certain button (Say, Mouse2), the equipped weapon cycles to another weapon in a predetermined order (say, if they're holding Musket then equip Crossbow). My idea was to detect this button press in the script that parses user input, then send the name of the weapon to another script, which would unattach Musket and attach Crossbow. Two problems jump to mind:

  1. Crossbow would need to be attached as a Child of Arm, itself a Child of Player

  2. Ammo, which is a variable stored in the Musket.cs/Crossbow.cs script, would need to be persisted and passed to the new Musket object when the player switches back to their Musket, otherwise the player would be able to refill ammunition by switching weapons, removing the point of their being ammunition in the first place.

(FirePoint is included in the Prefab, and isn't posing a problem when re-creating the Weapon).

I realise that this is a lengthy question, and appreciate any time spent in answering it.

Thanks

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

Answer by hav_ngs_ru · Jan 28, 2015 at 10:23 AM

if you dont want inactive weapon to bo visible - attach all of them to Arm and make them inactive. Then just activate one of them and deactivate all others. Script variables will store their values even after deactivate/activate objects (so you will hav correct ammo amount for each weapon).

if you want inactive weapon to stay visible (e.g. to hang behind or be placet to holster) - just set transform.parent of active weapon to Arm, and transform.parent of all other weapons to Holster. And just store link to active weapon somewhere to avoid shooting is your own leg :)))

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 Kantus · Jan 28, 2015 at 08:59 PM 0
Share

Thanks, it worked! One more question, though - Currently the swapping occurs instantaneously, such that if you hold down the swap button the weapon will swap at break-neck speed. This makes it difficult to swap to any particular weapon. Is there some way I could have the game remain sensitive to input, but to cease being sensitive to it between the time when the swap button is pressed down and the time when the swap button is released?

avatar image hav_ngs_ru · Jan 29, 2015 at 09:10 AM 0
Share

do you mean that you want to avoid multiple swapping weapon during one swap-button click? If so - you could use Input.GetButtonDown / Input.Get$$anonymous$$ouseButtonDown ins$$anonymous$$d of Input.GetButton / Input.Get$$anonymous$$ouseButton, these functions returns true only once, at moment you just pressed key (and return false all other time, even when you still hold buttons pressed).

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

21 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

Related Questions

How to pickup and then Change/Switch Weapons 1 Answer

Switching Weapons (C#) 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Weapon switching problem 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