- Home /
c# Weapon Pickup Script
Hello :) I am fairly new to Unity and i am trying to make a FPS. Currently, I am working on making the player able to pick up weapon off the floor, so far I have got it so the player can look at the object and press 'F' to make it disappear (just as a test, don't know if i need this) but I would like to make it so the player can pick up the weapon and add it to their inventory but I would like it so they can only hold two weapons at once and can switch between them. I am not sure what to do from here, any help (preferably in c#) would be appreciated. Thank You! In advance. :)
This is my code so far:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WeaponPickup : MonoBehaviour { public GameObject playerCamera; //this is a reference to the main camera (drag & drop) public GameObject weapon;
// Update is called once per frame
void Update()
{
RaycastHit hitInfo; //a structure to hold hit information (gameobject we hit, position of the "impact" etc.)
Ray r = new Ray(playerCamera.transform.position, playerCamera.transform.forward); //A ray starting from the camera, going forward
//if we hit something
if (Physics.Raycast(r, out hitInfo))
{
//if it is tagged as a weapon
if (hitInfo.transform.CompareTag("Weapon"))
{
//if the user presses F
if (Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Hi");
Destroy(weapon);
}
}
}
}
}
Hi @Oliver08 - "I am not sure what to do from here"
I think you can search here for more info - there are tons of questions on inventory system, item pickup, adding item to inventory and so on - did you google for these also? I'm asking because there are many good tutorials on this topic.
You are not asking to spot an error in your code you made, you are asking someone else to provide you with the missing code "(preferably in c#) " for your system.
Hi, i have searched but lots of them are in js not c# and most others i do not understand or am unsure how to implement them into what i have specifically .
Answer by niiicolai · Aug 20, 2018 at 06:19 PM
Hi @Oliver08 - You are on the right way. Here is an example that can help you on the way.
public class PickupManager {
public Transform cameraTransform;
public KeyCode pickupKey = KeyCode.F;
public KeyCode dropKey = KeyCode.G;
string weaponTag = "Weapon";
public List<GameObject> weapons;
public int maxWeapons = 2;
// this variable represent the weapon you carry in your hand
public GameObject currentWeapon;
// this variable represent your hand which you set as the parent of your currentWeapon
public Transform hand;
// Insert a gameobject which you drop inside your player gameobject and position it where you want to drop items from
// to avoid dropping items inside your player
public Transform dropPoint;
void Update() {
// SELECT WEAPONS
if (Input.GetKeyDown(KeyCode.Alpha1)) {
SelectWeapon(0);
}
if (Input.GetKeyDown(KeyCode.Alpha2)) {
SelectWeapon(1);
}
// PICKUP WEAPONS
RaycastHit hit;
Ray ray = new Ray(cameraTransform.position, cameraTransform.forward);
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.CompareTag(weaponTag) && Input.GetKeyDown(pickupKey) && weapons.Count < maxWeapons) {
// save the weapon
weapons.Add(hit.collider.gameObject);
// hides the weapon because it's now in our 'inventory'
hit.collider.gameObject.SetActive(false);
// now we can positioning the weapon at many other places.
// but for this demonstration where we just want to show a weapon
// in our hand at some point we do it now.
hit.transform.parent = hand;
hit.transform.position = Vector3.zero;
}
}
// DROP WEAPONS
// So let's say you wanted to add a feature where you wanted to drop the weapon you carry in your hand
if (Input.GetKeyDown(dropKey) && currentWeapon != null) {
// First ensure we remove our hand as parent for the weapon
currentWeapon.transform.parent = null;
// Move the weapon to the drop position
currentWeapon.transform.position = dropPoint.position;
// Remove it from our 'inventory'
var weaponInstanceId = currentWeapon.GetInstanceID();
for (int i = 0; i < weapons.Count; i++) {
if (weapons[i].GetInstanceID() == weaponInstanceId) {
weapons.RemoveAt(i);
break;
}
}
// Remove it from our 'hand'
currentWeapon = null;
}
}
void SelectWeapon(int index) {
// Ensure we have a weapon in the wanted 'slot'
if (weapons.Count > index && weapons[index] != null) {
// Check if we already is carrying a weapon
if (currentWeapon != null) {
// hide the weapon
currentWeapon.gameObject.SetActive(false);
}
// Add our new weapon
currentWeapon = weapons[index];
// Show our new weapon
currentWeapon.SetActive(true);
}
}
}
Now in order to add different settings to each weapon, you could add a weapon script to your weapons and use that to store stuff like firerate, maxbullets etc. and then store a reference to the weapon script on weapons you pick up instead of the gameobject.
Thank You So $$anonymous$$uch! Ill try this and let you know of any issues :)
Ok. Everything is working perfectly except a few things. When i drop the weapons they all go to the exact same place but face different ways depending on which way i am facing at the time. They also still have active scripts so when i fire with one gun, the dropped guns fire too. I am also able to pick up the weapons but when i switch to the slot that weapon is not in it just appears back at the drop point. Again, than you so much for the help so far
In order to answer why your gun keeps shooting you would have to show me your code. Did you add the weapon tag to your weapon? And does it have a collider?
Also your problem with the weapons dropping the same place. Did you add a gameobject inside your player gameobject and added to dropPoint? It's important that the dropPoint gameobject is a children of your player gameobject
I tagged all the weapons with 'Weapon and now when i pick up the weapon it comes to me but too far to the right in comparison to where it normally is.
Also, i have added an empty game object inside my player but no difference The weapons drop where my player is in the scene view before i play the game
can I ask when I use the code the code all perfect but when I try to play the weapon that I pick was deactive and than the weapon completely invisible. can you help me
Can you show us how you did that plz .. I'm new in unity
My gun is flying all over when I pick it back up. I don't have a animator help.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to delete prefabs without warning popping up? 0 Answers
2D Game has very bad lag because of one script? 0 Answers
Perfect inventory code giving errors? [C#] 1 Answer
Weird FPS Camera 0 Answers