c# Weapon Pickup Script

Hello :slight_smile: 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. :slight_smile:

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 - 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*.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.

My gun is flying all over when I pick it back up. I don’t have a animator help.