- Home /
swapping weapons
i have been working on a zombie game and i am having troubles. i want to make a gun script so when you get in the game you will start with a pistol and it has its own animations and shooting script then you can find another gun say a pp-2000 so you walk up to it and press "e" when in the area and it swaps your gun and changes the script and gun sound for that weapon for you to use. I don't know how to do that and i was wondering if someone can help me with that.
thank you for reading what I have to say.
I'm pretty new to Unity so my advice might not be the best. But you could try making prefabs for all the guns you want (pp-2000,pistol, m4, etc...) and include animations, scripts and audio in their prefabs.
Then (if it's first-person) use Raycasting from your camera to objects to check if they're within range. And if they are use an if statement with "($$anonymous$$eyCode.E)" to intsantiate the new weapon prefab /destroy the old prefab.
If you swap the active prefabs it would also switch the scripts, audio, etc.
Here's a pretty good tutorial for Raycasting: http://www.youtube.com/watch?v=P0PHY1hJp5k
Again, as I said, I'm pretty new to Unity so sorry if this isn't what you wanted.
ok thankyou for the information about that that was a good idea creating prefabs all i need now is how to make the script to pick up the gun on the ground and swap it out
Scripting part I can't help with much in detail, because I'm still learning to script myself. But as I said I think raycasting would be necessary to deter$$anonymous$$e distance. If you're making a FPS you could use (I don't know the exact name but it's something like)ray.distance, and then always have a ray shooting from your fps camera, and then if the distance is like less than 5 units away prompt a key to switch weapons. Not sure what the best way to switch weapons would be, but maybe google can help?
Answer by PvTGreg · Mar 03, 2014 at 08:53 AM
for picking up im sure you can edit this to your liking this adds it to an inventory but youc an take out bits you need and use it
pragma strict
//Assign this script to an Item if you want to pick it up in First Person. If this script is not attached the Item can only be picked up when clicking on it with the mouse.
var InstructionBoxSkin : GUISkin; //The skin to use. Default one is 'OtherSkin' under the 'Resources' folder.
var ButtonToPress : KeyCode = KeyCode.E; //The button to press when picking up the item.
var PickUpDistance = 1.7f; //The distance from where the Item can be picked up. Remember that this is relative to the center of the Item and the center of the Player.
//These store information about the Item, if we can pick it up, the Player and the distance to the Player.
private var canPickUp = false;
private var theItem : Item;
private var thePlayer : Transform;
private var dist = 9999f;
@script AddComponentMenu ("Inventory/Items/First Person Pick Up")
@script RequireComponent(Item)
//This is where we find the usefull information which we can later access.
function Awake ()
{
theItem = (GetComponent(Item));
if (InstructionBoxSkin == null)
{
InstructionBoxSkin = Resources.Load("OtherSkin", GUISkin);
}
}
function RetrievePlayer (theInv : Inventory)
{
thePlayer = theInv.transform.parent;
}
function OnGUI ()
{
//This is where we draw a box telling the Player how to pick up the item.
GUI.skin = InstructionBoxSkin;
GUI.color = Color(1, 1, 1, 0.7);
if (canPickUp == true)
{
if (transform.name.Length <= 7)
{
GUI.Box (Rect (Screen.width*0.5-(165*0.5), 200, 165, 22), "Press E to pick up " + transform.name + ".");
}
else
{
GUI.Box (Rect (Screen.width*0.5-(185*0.5), 200, 185, 22), "Press E to pick up " + transform.name + ".");
}
}
}
function Update ()
{
if (thePlayer != null)
{
//This is where we enable and disable the Players ability to pick up the item based on the distance to the player.
dist = Vector3.Distance(thePlayer.position, transform.position);
if (dist <= PickUpDistance)
{
canPickUp = true;
}
else
{
canPickUp = false;
}
//This is where we allow the player to press the ButtonToPress to pick up the item.
if (Input.GetKeyDown(ButtonToPress) && canPickUp == true)
{
theItem.PickUpItem();
}
}
}
//This is just for drawing the sphere in the scene view for easy testing.
function OnDrawGizmosSelected ()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere (transform.position, PickUpDistance);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
gui help for ammo display 1 Answer
I Need Help Whit Gun! 2 Answers
Help with gun scripting!? 1 Answer