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 Scribe · Feb 28, 2011 at 07:54 PM · javascriptchangeweaponswapping

Weapon swapping in-game

Hi there I am planning on having around 15 different guns within my game and am wondering how to change weapons from the one in my hand to a weapon on the ground. I have a firing/ammo script for my guns and I have a gun swapping script that I made:

var MyHand : Transform; //your hand var MyGun : Transform; //what your picking up var ThatGun : Transform; var dist = 5; //distance at which you can pick things up private var isHolding = false;

 function Update () {
     if(Input.GetKeyDown(KeyCode.Q)){
         if(Vector3.Distance(transform.position, MyGun.position) < dist)
         {
             isHolding = !isHolding;
             }
             }

     if(isHolding == true){
         MyHand.transform.DetachChildren();
         MyGun.rigidbody.useGravity = false;
         MyGun.parent = MyHand;
         MyGun.transform.position = MyHand.transform.position;
         MyGun.transform.rotation = MyHand.transform.rotation;
         }
         else{
             MyHand.transform.DetachChildren();
             MyGun.rigidbody.useGravity = true;
             ThatGun.parent = MyHand;
             ThatGun.transform.position = MyHand.transform.position;
             ThatGun.transform.rotation = MyHand.transform.rotation;
         }
 }

the problem is that this script that I made-up only works for two guns and I need to swap between around 15 eventually. I know I could extend this script to work for more guns but I'm worried it would get very complicated and slow functioning.

Any help would be much appreciated

Thanks, Scribe (javascript please)

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

Answer by Bunny83 · Feb 28, 2011 at 09:00 PM

You are testing against a particular gun that you have to assign to your player. Just tag your guns as "Gun" and do something like that:

var MyHand : Transform; var CurrentGun : Transform; var dist : float = 5;

function Update() { if (Input.GetKeyDown(KeyCode.Q)){ var allNearObjects : Collider[] = Physics.OverlapSphere(transform.position,dist); for (var current : Collider in allNearObjects) { if (current.transform != CurrentGun && current.tag == "Gun") { // Detach old gun CurrentGun.parent = null; CurrentGun.rigidbody.isKinematic = false; //Attach new gun CurrentGun = current.transform; CurrentGun.rigidbody.isKinematic = true; CurrentGun.parent = MyHand; CurrentGun.localPosition = Vector3.zero; CurrentGun.localRotation = Quaternion.identity; break; } } } }

When hitting the "Q" button it iterated through all colliders that are in range (dist). If it found a gameObject that is tagged as "Gun" and it's not the one you're using at the moment. switch them.

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 Scribe · Mar 01, 2011 at 04:50 PM 0
Share

thanks worked great

avatar image
2

Answer by Justin Warner · Feb 28, 2011 at 08:13 PM

Make an array of guns, and just have it change due to amount, and have a switch statement for certain values...

A = 0, B=1, C=2, D=3.

Switch so that if A, gun value = 0, and so you change var gun to that gun, and all the properties... B has value = 1 etc.

Kind of easy.

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

No one has followed this question yet.

Related Questions

Player Animation change when pickup Weapon 0 Answers

I'm trying to add a 3rd and possible more weapons to this script what am I missing?? 1 Answer

Mario Kart Style random weapon 3 Answers

change detail in game an the sound? 3 Answers

How can I change an objects texture through javascript? 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