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 TheRealPinecone · Aug 20, 2016 at 02:14 PM · c#2d2d-platformerplatformerbasic programming

How do i make a one gun only Weapon system. (2D)

I am working on just a personal project to share with family and friends and it is a 2D platformer. The issue is that I can't figure out if this code is right and if it is how should I translate it in C#? The code is: If player walks over weapon, ask player if he wants weapon (in GUI), If he say yes, Move weapon to player hand. Questions are: Would I need a inventory system for this where I place all the weapons (which are alot) in the player's inventory but they aren't accessable (keep in mind this is no rpg so inevntory can't be accessed) and when player says yes it just unlocks the specific weapon in inventory and disables the current one? And one last question (srry I am a bit new to this) is there a way I can player spawn with a specific weapon in a every level? Help is much appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by 334499p · Aug 21, 2016 at 08:37 PM

Hi @TheRealPinecone, the way I do it in my games is to have 2 versions of each weapon:

  • Prefab 1: Kept on the ground and deleted when the player agrees to picking it up

  • Prefab 2: Instantiated directly into the player's hand when prefab 1 is deleted (picked up)

If you can't switch weapons that you have picked up then you shouldn't need to keep track of weapons that you have picked up. Simply delete the instantiated weapon from your hand (second prefab) and instantiate a new version of the weapon you picked up.

Each weapon that you pickup should have an ID (you can keep this in a script for all prefab1 weapons) so that you know what weapon from an array of prefab2s to instantiate.

If you want to start each level with a weapon then just instantiate prefab 2 into the player's hand.

 public Transform playerHand;
 //Weapons placed into the prefab arrays should line up: If you have a shotgun in prefab1[0], 
 //make sure the player version of the weapon is in prefab2[0]
 
 public GameObject[] prefab1;
 public GameObject[] prefab2;
 
 GameObject instantiatedWeapon;
 
 void Start(){
 instantiatedWeapon = (GameObject)Instantiate(prefab2[weaponID],playerHand.position,Quaternion.identity);
 instantiatedWeapon.transform.parent = playerHand;
 instantiatedWeapon.transform.localPosition = Vector3.zero; //Make sure the weapon is in the player's hand
 instantiatedWeapon.transform.localRotation = Quaternion.identity; //Make sure the weapon is rotated properly
 }

If you want to instantiate a new weapon if a prefab1 is picked up:

 void pickUpWeapon(int weaponID){
 if(instantiatedWeapon != null){ //If we have a weapon in our hand
 Destroy(instantiatedWeapon);
 }
 instantiatedWeapon = (GameObject)Instantiate(prefab2[weaponID],playerHand.position,Quaternion.identity);
 instantiatedWeapon.transform.parent = playerHand;
 instantiatedWeapon.transform.localPosition = Vector3.zero;
 instantiatedWeapon.transform.localRotation = Quaternion.identity; 
 }



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 TheRealPinecone · Aug 24, 2016 at 10:52 AM

@334499p , THANKS SOOO MUCH. Sorry it took me this long to reply, my email didn't let me open the forum for some reason. But you helped me soo much, it is unbelieveable. This is was one of the only issues I was faceing when creating a platformer, Thanks again, Now I can actually carry on the project.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to get Player Tip for player to open on Return and close on Return 2 Answers

Player Tips - Have triggers act independently 1 Answer

2D Flashlight for a platformer 2 Answers

Player stucken in the wall while jumping 2D 1 Answer

Double jump 2D Platformer 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