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 Thomas Paniagua · Apr 25, 2011 at 05:56 PM · fpsweaponfirst-personweaponchanging

Any help with a weapon changing script?

Hey guys, I have some experience with C#, Javascript and PHP, but not with any graphical interface. My question is if somebody can help me displaying a weapon model in certain place of my camera, but I don't know how to do that. Can anybody teach me how to do it? How to store the gameObjects in an array in the player script and then display one of the game objects in the array or something like that? But I don't want the weapon to be exactly in from of the player, I want it to appear as if the player was holding it. PLEASE HELP!!!
PS I don't mean with an animation when I say the player holding the weapon I mean just position the weapon.

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

Answer by oliver-jones · Apr 25, 2011 at 06:17 PM

Okay, you have 3 mains things going on here:

1- Actual Object of your weapons (eg: assault rifle, pistol, and frag) 2- Script that holds the weapons 3- the user interface - so a heads up display (Unity call it a GUI)

Step one would be something like this:

Drag your model of your assault rifle onto your scene, and place it into a empty object and call it assault rifle. Then, make sure your model is set to 0,0,0 axis within the empty object. Place your assault rifle into your camera within your character controller.

So you should have something like this in your hierarchy: FirstPersonController > MainCamera > Assault Rifle > Model (remember: with the model being at 0,0,0)

Next, move your Assault Rifle (the empty object) so it looks like your character is holding it correctly and is on screen.

Do this repeatedly with your pistol ... and your frag? (maybe having a model of an arm holding a frag??)

So you should have 3 objects under your camera: Assault rifle, pistol and frag all in the correct position.

-- Now time for the script (Java Script)--

This is basically going to be an inventory. So you want something like this:

var assaultRifle : GameObject;
var pistol : GameObject;
var frag : GameObject;

-- or if you want an array (which is slightly more complicated):

var weapons : GameObject[];

By the way - call this script WeaponInventory or something and drag it onto your Camera. Then, drag your assault rifle game object onto the var : assaultRifle in the Inspector. Or, if you did the array, drag it on that ... for now, I wont be using the array.

So like I said: drag your weapons corresponding to the variables we just made.

Go back to your script and add a start function:

function Start(){
//we want to define our first choice of weapon - this case will be the assault rifle
assaultRifle.gameObject.SetActiveRecursively(true);
pistol.gameObject.SetActiveRecursively(true);
frag.gameObject.SetActiveRecursively(true);
//this will deactivate the pistol and frag, and leave on the assault rifle
}

Next is to change the weapons depending on the Key Input:

function Update(){ if(Input.GetKeyDown("1")){ //assault key killAll(); assaultRifle.gameObject.SetActiveRecursively(true); }

if(Input.GetKeyDown("2")){ //pistol key killAll(); pistol.gameObject.SetActiveRecursively(true); }

if(Input.GetKeyDown("3")){ //frag key killAll(); frag.gameObject.SetActiveRecursively(true); } }

function killAll(){ assaultRifle.gameObject.SetActiveRecursively(false); pistol.gameObject.SetActiveRecursively(false); frag.gameObject.SetActiveRecursively(false); }

This is kind of rushed but it gets the job done. If you want animation of the character 'putting' the weapon down, and then selecting his next one this you just do a play.animation.

Hope that kinda helps.

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 eeveelution8 · Mar 10, 2013 at 09:07 PM 0
Share

totally works, really cool man :)

avatar image knuckles209cp · Jan 27, 2014 at 05:51 AM 0
Share

whoa i never thought of it that way

avatar image
0

Answer by Mr_Grr · May 31, 2014 at 11:12 AM

heres the relevant unity 4.5 C# code, works well for me, as you can tell i changed a few names.

 using UnityEngine;
 using System.Collections;
 
 public class WeaponControl : MonoBehaviour {
 
     public GameObject pulse;
     public GameObject beam;
     public GameObject shield;
 
     // Use this for initialization
     void Start () {
         pulse.gameObject.SetActive(true);
         beam.gameObject.SetActive(false);
         shield.gameObject.SetActive(false);
     }
     
     // Update is called once per frame
     void Update(){
         if(Input.GetKeyDown("1")){
             //assault key
             killAll();
             pulse.gameObject.SetActive(true);
         }
         
         if(Input.GetKeyDown("2")){
             //pistol key
             killAll();
             beam.gameObject.SetActive(true);
         }
         
         if(Input.GetKeyDown("3")){
             //frag key
             killAll();
             shield.gameObject.SetActive(true);
         }
     }
     
     void killAll(){
         pulse.gameObject.SetActive(false);
         beam.gameObject.SetActive(false);
         shield.gameObject.SetActive(false);
     }
 }
 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Weaponbob for first person shooter 1 Answer

Is there a way to make a selected object a child of another object during the game? 1 Answer

problem with weapon animation -.- 0 Answers

fps multiplayer camera set (posible script) 1 Answer

Weapon Switching 2 Answers


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