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 twoface262 · Jan 12, 2012 at 02:34 AM · javascriptweaponswitching

Weapon switching.

Hello I'm trying to code a way to switch weapons. I'm new to unity and am very confused. I have this from the fps tutorial and a bit of what I did.

#pragma strict

var wepNum : float = 0; var numOfWeps : float = 5;

function Start () {

}

function Update () { switchgun(wepNum);

if(Input.GetKeyDown("1")) { if(wepNum > 0) { wepNum--; } } if(Input.GetKeyDown("2")) { if(wepNum < numOfWeps) { wepNum++; } } } function switchgun(index : int) { for (var i=0;i < transform.childCount;i++) {

if (i == index)

transform.GetChild(i).gameObject.SetActiveRecursively(true);

else transform.GetChild(i).gameObject.SetActiveRecursively(false); } }

but how would I switch weapon models? and I don't really understand what this does.

function switchgun(index : int) { for (var i=0; i < transform.childCount;i++) {

if (i == index)

transform.GetChild(i).gameObject.SetActiveRecursively(true);

else transform.GetChild(i).gameObject.SetActiveRecursively(false); } }

Comment
Add comment · Show 2
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 syclamoth · Jan 12, 2012 at 03:01 AM 0
Share

For starters, I'm pretty sure 'wepNum' and 'numOfWeps' are both supposed to be integers, since they are for counting things. How much do you actually understand about how this script works?

avatar image twoface262 · Jan 12, 2012 at 03:07 AM 0
Share

I understand how most of it works. I just don't understand that last function. Also how would I switch models with each time I switch guns?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jan 12, 2012 at 03:15 AM

You must attach this script to an empty object and child the empty object to the camera, then child all weapon models to this empty object, and adjust each one to the desired position/rotation. The function switchgun(index) just deactivates all weapons, except the one with index equal to index. When and object is deactivated, it becomes invisible and completely inactive, thus only one weapon will appear and obey the commands.

 function SwitchGun(index : int)
 {
   for (var i=0;i < transform.childCount;i++)
   { // iterate through all childed weapons:
     if (i == index) // if it's the selected index, activate it:
       transform.GetChild(i).gameObject.SetActive(true);
     else // if it's any other index, deactivate it:
       transform.GetChild(i).gameObject.SetActive(false);
   }
 }

Download the First Person Tutorial Completed and you will see the character with its two weapon models childed to it.

EDITED: Function updated to Unity 4, which uses SetActive instead of the deprecated SetActiveRecursively. By the way, the FPS Tutorial is no longer available at Unity's site, but can be downloaded from http://visionquest3d.com/slam/FPS_TutorialsOld.zip

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 twoface262 · Jan 12, 2012 at 03:17 AM 0
Share

ohh... I see now. Thank you so much I can't wait to get back to coding! Unity is a great engine this my first time working in a 3d engine I've worked in a lot of 2d engines before but I wanted to take a step up and decided to take a good approach. Thanks for the help!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weapon Switching 2 Answers

Weapon Switching 2 Answers

weapon sweep 1 Answer

Switching Between Cameras JavaScript 0 Answers

Problem With Weapon Firing Mechanism 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