- Home /
Help needed in changing weapons
hello everyone
i am in the process of making a weapon switching/looting script
here is how my system works: player interacts with the item of the floor.
the script gets a message that the player has the item = true. The GUI button for the item is drawn. If the button is pressed then the item is shown as your current equipped item.
using UnityEngine;
using System.Collections;
public class MainGUI : MonoBehaviour
{
public bool _displayinventory = true;
public bool _displaycharacterwindow;
public GUISkin myskin;
public GameObject curwep;
public GameObject PhotonCanon;
public GameObject RxBlaster;
public static bool _hasPhotonCanon = false;
public static bool _hasRxblaster = false;
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
GUI.skin = myskin;
if (_hasPhotonCanon) {
//Debug.Log ("you have obtained a photon canon");
if (GUI.Button (new Rect (460, 750, 90, 70), "", "photoncanon")) {
curwep = PhotonCanon;
Debug.Log ("i iwll equip to you" + curwep.ToString ());
}
}
if (_hasRxblaster) {
//Debug.Log ("you have obtained a Rx-Blaster");
if (GUI.Button (new Rect (570, 750, 90, 70), "", "Rx-blaster")) {
Debug.Log ("i wll equip to you RX ON !!!!" + curwep.ToString ());
curwep = RxBlaster;
}
}
}
}
Here is the problem. first of all i am trying to make it so that the previous item that i had equipped turns off ( the gameobject turns off) and then the new item that has now been assigned to be the current weapon (curwep) turns on (the gamobject turns on).
I have attached my code. Can someone please take a look at this and please tell me how i should approach what i am trying to do
by the way this is all written in c# for those of you who haven't noticed.
Thanks in advance
Regards
Your code format didn't work out ( see it all the time , it's some selection glitch) Could you please fix that so all code is in the format block? Tx in advance!
Answer by Robert Attard · Jul 16, 2011 at 01:24 AM
You can create the weapons as prefabs and attach them to a character bone. Attach this script to your game character
public class Player : MonoBehaviour
{
//attach 2 weapon prefabs to weapon1 and weapon2
//attach a bone Transform (head, foot, hand etc..) from your character to bone;
public Transform weapon1;
public Transform weapon2;
public Transform bone;
private Transform currentweapon;
void Start()
{
//we attach weapon1 first;
currentweapon = Instantiate(weapon1, bone.position, bone.rotation) as Transform;
currentweapon.parent = bone;
}
public void removeCurrentWeapon()
{
currentweapon.parent = null;
Destroy(currentweapon.transform.gameObject);
}
void Update()
{
//press the right control button will swap between weapon1 and weapon2
// this is just for testing purposes, you will obviously have to do something much more
// sophisticated
if (currentweapon != null)
{
if (currentweapon.name == "weapon1(Clone)")
{
removeCurrentWeapon();
currentweapon = Instantiate(weapon2, bone.position, bone.rotation) as Transform;
currentweapon.parent = bone;
}
else
{
removeCurrentWeapon();
currentweapon = Instantiate(weapon1, bone.position, bone.rotation) as Transform;
currentweapon.parent = bone;
}
}
}
}
I fully tested it so it should work..
I also done a quick video to show you the above code in action http://www.youtube.com/watch?v=VH0ZfEv_ouc
Answer by Ermarrero · Apr 05, 2014 at 04:30 AM
Try this in update () .. cheers.
if (_hasPhotonCanon == true) _hasRxblaster = false;
else _hasPhotonCanon = false;