- Home /
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.
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.
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);
}
}