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
0
Question by zalanthos · Mar 02, 2014 at 02:29 PM · gungun script

swapping weapons

i have been working on a zombie game and i am having troubles. i want to make a gun script so when you get in the game you will start with a pistol and it has its own animations and shooting script then you can find another gun say a pp-2000 so you walk up to it and press "e" when in the area and it swaps your gun and changes the script and gun sound for that weapon for you to use. I don't know how to do that and i was wondering if someone can help me with that.

thank you for reading what I have to say.

Comment
Add comment · Show 4
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 Destran · Mar 02, 2014 at 04:53 PM 0
Share

I'm pretty new to Unity so my advice might not be the best. But you could try making prefabs for all the guns you want (pp-2000,pistol, m4, etc...) and include animations, scripts and audio in their prefabs.

Then (if it's first-person) use Raycasting from your camera to objects to check if they're within range. And if they are use an if statement with "($$anonymous$$eyCode.E)" to intsantiate the new weapon prefab /destroy the old prefab.

If you swap the active prefabs it would also switch the scripts, audio, etc.

Here's a pretty good tutorial for Raycasting: http://www.youtube.com/watch?v=P0PHY1hJp5k

Again, as I said, I'm pretty new to Unity so sorry if this isn't what you wanted.

avatar image zalanthos · Mar 03, 2014 at 07:05 AM 0
Share

ok thankyou for the information about that that was a good idea creating prefabs all i need now is how to make the script to pick up the gun on the ground and swap it out

avatar image Destran · Mar 03, 2014 at 08:33 AM 0
Share

Scripting part I can't help with much in detail, because I'm still learning to script myself. But as I said I think raycasting would be necessary to deter$$anonymous$$e distance. If you're making a FPS you could use (I don't know the exact name but it's something like)ray.distance, and then always have a ray shooting from your fps camera, and then if the distance is like less than 5 units away prompt a key to switch weapons. Not sure what the best way to switch weapons would be, but maybe google can help?

avatar image zalanthos · Mar 04, 2014 at 05:07 AM 0
Share

oh ok thankyou for your help

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PvTGreg · Mar 03, 2014 at 08:53 AM

for picking up im sure you can edit this to your liking this adds it to an inventory but youc an take out bits you need and use it

pragma strict

 //Assign this script to an Item if you want to pick it up in First Person. If this script is not attached the Item can only be picked up when clicking on it with the mouse.
 
 var InstructionBoxSkin : GUISkin; //The skin to use. Default one is 'OtherSkin' under the 'Resources' folder.
 var ButtonToPress : KeyCode = KeyCode.E; //The button to press when picking up the item.
 var PickUpDistance = 1.7f; //The distance from where the Item can be picked up. Remember that this is relative to the center of the Item and the center of the Player.
 
 //These store information about the Item, if we can pick it up, the Player and the distance to the Player.
 private var canPickUp = false;
 private var theItem : Item;
 private var thePlayer : Transform;
 private var dist = 9999f;
 
 @script AddComponentMenu ("Inventory/Items/First Person Pick Up")
 @script RequireComponent(Item)
 
 //This is where we find the usefull information which we can later access.
 function Awake ()
 {
     theItem = (GetComponent(Item));
     
     if (InstructionBoxSkin == null)
     {
         InstructionBoxSkin = Resources.Load("OtherSkin", GUISkin);
     }
 }
 
 function RetrievePlayer (theInv : Inventory)
 {
     thePlayer = theInv.transform.parent;
 }
 
 function OnGUI ()
 {
     //This is where we draw a box telling the Player how to pick up the item.
     GUI.skin = InstructionBoxSkin;
     GUI.color = Color(1, 1, 1, 0.7);
     
     if (canPickUp == true)
     {
         if (transform.name.Length <= 7)
         {
             GUI.Box (Rect (Screen.width*0.5-(165*0.5), 200, 165, 22), "Press E to pick up " + transform.name + ".");
         }
         else
         {
             GUI.Box (Rect (Screen.width*0.5-(185*0.5), 200, 185, 22), "Press E to pick up " + transform.name + ".");
         }
     }
 }
 
 function Update ()
 {
     if (thePlayer != null)
     {
         //This is where we enable and disable the Players ability to pick up the item based on the distance to the player.
         dist = Vector3.Distance(thePlayer.position, transform.position);
         if (dist <= PickUpDistance)
         {
             canPickUp = true;
         }
         else
         {
             canPickUp = false;
         }
         
         //This is where we allow the player to press the ButtonToPress to pick up the item.
         if (Input.GetKeyDown(ButtonToPress) && canPickUp == true)
         {
             theItem.PickUpItem();
         }
     }
 }
 
 //This is just for drawing the sphere in the scene view for easy testing.
 function OnDrawGizmosSelected () 
 {
     Gizmos.color = Color.yellow;
     Gizmos.DrawWireSphere (transform.position, PickUpDistance);
 }
Comment
Add comment · 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 zalanthos · Mar 04, 2014 at 05:08 AM 0
Share

ah thanks i shall try it later

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

22 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

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

gui help for ammo display 1 Answer

I Need Help Whit Gun! 2 Answers

Help with gun scripting!? 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