Here I will explain how I make it so that you have two weapons on 1 and 2 and can replace them by picking up others.
So what you have to do beforehand is setting up several tags with the names of the weapons like for example “AK”, “M16”,…
The weaponshave to be a child of either an empty gameobject or the camera like it is in for example the FPS tutorial.
Now you have to set the weapons to the fitting tags. (Btw. In my game I have an empty gameobject called “Weapons” which is a child of the playercamera. The Weapons are children of “Weapons”. Every weapon consist of an empty gameobject with the shoting script. The weaponmodel and the necessary particles are children of this gameobject. Just to clear things up).
Then you make prefabs of Weaponmodel to pick up. The prefabs also has to have the fitting tags.
So here is my PlayerWeapons script which is attached to the “Weapons”-GameObject. I changed it slightly because I have some stuff in it which is not important for you.
static var PrimaryWeapon="AK";
static var SecondaryWeapon="DesertEagle";
static var SwitchWeapon=1;
function Start () {
// Select the first weapon
SelectWeapon(0);
PlayerCharContr= Player.GetComponent("CharacterController");
}
function Update () {
//Here I deleted all of the stuff from Firing the Weapons to Crouching, Grenade-Throwing,...
if(Input.GetKeyDown("1"))
{
SwitchWeapon=1;
SelectWeaponTag(PrimaryWeapon);
}
if(Input.GetKeyDown("2"))
{
SwitchWeapon=2;
SelectWeaponTag(SecondaryWeapon);
}
}
function SelectWeapon (index : int) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
{
transform.GetChild(i).gameObject.SetActiveRecursively(true);
transform.GetChild(i).gameObject.BroadcastMessage("ChangeWeapon");
// Deactivate all other weapons
}
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
function SelectWeaponTag (tag: String) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (transform.GetChild(i).gameObject.tag==tag)
{
print(tag);
transform.GetChild(i).gameObject.SetActiveRecursively(true);
transform.GetChild(i).gameObject.BroadcastMessage("ChangeWeapon", SendMessageOptions.DontRequireReceiver);
// Deactivate all other weapons
}
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
I hope I didn’t deleted anything important or left something unimportant in.
Now here is the script to Pickup Weapons:
var WeaponsArray= new Array ();
var WeaponPickuppref: GameObject;
function Start () {
WeaponsArray.length= transform.childCount;
for (var i=0;i<transform.childCount;i++) {
WeaponsArray*= transform.GetChild(i).tag;*
}
function UseFunc () {
- var hit : RaycastHit;*
- var cam : Transform = Camera.main.transform;*
- var layerMask : int = 1 << 8;*
- layerMask = ~layerMask;*
_ if (Physics.Raycast (cam.position+cam.transform.forward.normalized*1, cam.forward, hit, 5, layerMask))_
- {*
-
if(hit.rigidbody)*
-
var SelectedWeapon = hit.rigidbody.tag;*
-
//print(SelectedWeapon);*
-
//Destroy(hit.rigidbody.gameObject);*
-
for (var i=0;i<transform.childCount;i++) {*
_ if(SelectedWeapon==WeaponsArray*){*_
* ThrowWeapon(cam);*
* if(PlayerWeapons.SwitchWeapon==1)*
* {*
* PlayerWeapons.PrimaryWeapon=SelectedWeapon;*
* }*
* else if(PlayerWeapons.SwitchWeapon==2)*
* {*
* PlayerWeapons.SecondaryWeapon=SelectedWeapon;*
* }*
* transform.SendMessage(“Pickupfunc”, SendMessageOptions.DontRequireReceiver);*
* Destroy(hit.rigidbody.gameObject);*
* }*
* }*
* } *
}
function ThrowWeapon(cam:Transform){
* var rotation=Quaternion(cam.forward.x,cam.forward.y,cam.forward.z,0);*
* if(PlayerWeapons.SwitchWeapon==1)*
* {*
* for(var p=0;p<WeaponPickuppref.length;p++){*
* if(WeaponPickuppref[p].tag==PlayerWeapons.PrimaryWeapon){*
_ var ThrowedWeapon1:Rigidbody=Network.Instantiate(WeaponPickuppref[p].rigidbody, cam.position+cam.transform.forward.normalized2, rotation,0);
ThrowedWeapon1.AddForce(cam.forward10);
* }*
* }*
* }*
* if(PlayerWeapons.SwitchWeapon==2)*
* {*
* for(var p2=0;p2<WeaponPickuppref.length;p2++){*
* if(WeaponPickuppref[p2].tag==PlayerWeapons.SecondaryWeapon){*
var ThrowedWeapon2:Rigidbody=Network.Instantiate(WeaponPickuppref[p2].rigidbody, cam.position+cam.transform.forward.normalized2, rotation,0);
ThrowedWeapon2.AddForce(cam.forward10); _
* }*
* }*
* } *
}
Here I also hope it works because I had to delete some stuff in this one too.
I really hope this helps if something is missing or is not working then let me know and I see what I can do.
Good Luck!