How do I fix my player?

I’ve made this script to control my players weapons, inventory and pickups. I don’t want to use multiple scripts for each because plan on adding coop and I want it to be modular. I’m not getting any errors and it seems to work fine other than the pickups, weapon selection and every time the game starts it makes the gravity gun model visible but have the effects of the rifle.

Code:

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Runtime.CompilerServices;
using Unity.Entities;
using Unity.VisualScripting;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;

public class PlayerManager : MonoBehaviour
{
    [Header("HUD")]
    public GameObject mainWeapon;
    public GameObject sidearmWeapon;
    public GameObject utilityItem;
    public GameObject pickupText;

    [Header("Abilities")]
    public GameObject Dash;
    public GameObject Hack;

    [Header("Inventory")]
    public GameObject inventory;
    public GameObject helmet;
    public GameObject boots;
    public GameObject chestpiece;

    [Header("Weapons")]
    public GameObject Rifle;
    public GameObject Pistol;
    public GameObject Minigun;
    public GameObject Sword;
    public GameObject ToxinLauncher;
    public GameObject GravityGuantlet;
    public GameObject RailGunPistol;

    [Header("Gun Script")]
    public GameObject GunScript;
    public Prefab standardLaser;
    public Prefab gravityLaser;
    public Prefab toxinLaser;
    public Prefab projectile;

    [Header("Player Stats")]
    public float Health = 100f;
    public float Speed = 11.5f;
    public float runSpeed = 15.5f;
    public float jumpForce = 7f;
    public float Damage = 10f;

    [Header("Utility Items")]
    public GameObject impluseGrenade;
    public GameObject repairKit;
    public GameObject advancedRepairKit;
    public GameObject grapple;
    public GameObject impactGrenade;
    public GameObject empGrenade;
    public GameObject jetPack;

    // private bools

    private bool curMWep;
    private bool curSWep;
    private bool selectedWeapon;
    private bool curUtil;
    private bool mwepchange;

    // Start
    void Start()
    {
        curMWep = Rifle;
        curSWep = Pistol;
        pickupText.SetActive(false);

        selectedWeapon = Rifle;
        Rifle.SetActive(true);
    }

    // Update
    void Update()
    {
        WeaponSelector();
        selectWeapons();
    }

    // Inventory
    public void Inventory()
    {
        if (Input.GetKeyDown(KeyCode.Tab) && inventory != null)
        {
            inventory.SetActive(true);
        }
    }

    // Abilities
    public void Ability()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            Debug.Log("Dash was used.");
        }

        if (Input.GetKeyDown(KeyCode.Q))
        {
            Debug.Log("Hack was used.");
        }        
    }


    private void OnTriggerEnter(Collider other)
    {
        pickupText.SetActive(true);
        if (other.CompareTag("RiflePickup") && Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Player picked up rifle!");
            Destroy(other);
            curMWep = Rifle;
        }

        if (other.CompareTag("PistolPickup") && Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Player picked up Pistol!");
            Destroy(other);
            curMWep = Pistol;
        }

        if (other.CompareTag("MinigunPickup") && Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Player picked up Minigun!");
            Destroy(other);
            curMWep = Minigun;
        }

        if (other.CompareTag("SwordPickup") && Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Player picked up Sword!");
            Destroy(other);
            curMWep = Sword;
        }

        if (other.CompareTag("RailgunPickup") && Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Player picked up Railgun!");
            Destroy(other);
            curMWep = RailGunPistol;
        }

        if (other.CompareTag("ToxinPickup") && Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Player picked up Toxin Launcher!");
            Destroy(other);
            curMWep = ToxinLauncher;
        }

        if (other.CompareTag("GraivtyGunPickup") && Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Player picked up Gravity Guantlet!");
            Destroy(other);
            curMWep = GravityGuantlet;
        }

        // Side Arm Weapon Pickups
        if (other.CompareTag("RiflePickup") && Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("Player picked up rifle!");
            Destroy(other);
            curSWep = Rifle;
        }

        if (other.CompareTag("PistolPickup") && Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("Player picked up Pistol!");
            Destroy(other);
            curSWep = Pistol;
        }

        if (other.CompareTag("MinigunPickup") && Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("Player picked up Minigun!");
            Destroy(other);
            curSWep = Minigun;
        }

        if (other.CompareTag("SwordPickup") && Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("Player picked up Sword!");
            Destroy(other);
            curSWep = Sword;
        }

        if (other.CompareTag("RailgunPickup") && Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("Player picked up Railgun!");
            Destroy(other);
            curSWep = RailGunPistol;
        }

        if (other.CompareTag("ToxinPickup") && Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("Player picked up Toxin Launcher!");
            Destroy(other);
            curSWep = ToxinLauncher;
        }

        if (other.CompareTag("GraivtyGunPickup") && Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("Player picked up Gravity Guantlet!");
            Destroy(other);
            curSWep = GravityGuantlet;
        }
    }

    public void WeaponSelector()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            selectedWeapon = curMWep;
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            selectedWeapon = curSWep;
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            UseUtility();
        }
    }

    public void selectWeapons()
    {
        if (selectedWeapon == Rifle)
        {
            Rifle.SetActive(true);
            Pistol.SetActive(false);
            Minigun.SetActive(false);
            ToxinLauncher.SetActive(false);
            Sword.SetActive(false);
            RailGunPistol.SetActive(false);
            GravityGuantlet.SetActive(false);
        }

        if (selectedWeapon == Pistol)
        {
            Rifle.SetActive(false);
            Pistol.SetActive(true);
            Minigun.SetActive(false);
            ToxinLauncher.SetActive(false);
            Sword.SetActive(false);
            RailGunPistol.SetActive(false);
            GravityGuantlet.SetActive(false);
        }

        if (selectedWeapon == Minigun)
        {
            Rifle.SetActive(false);
            Pistol.SetActive(false);
            Minigun.SetActive(true);
            ToxinLauncher.SetActive(false);
            Sword.SetActive(false);
            RailGunPistol.SetActive(false);
            GravityGuantlet.SetActive(false);
        }

        if (selectedWeapon == ToxinLauncher)
        {
            Rifle.SetActive(false);
            Pistol.SetActive(false);
            Minigun.SetActive(false);
            ToxinLauncher.SetActive(true);
            Sword.SetActive(false);
            RailGunPistol.SetActive(false);
            GravityGuantlet.SetActive(false);
        }

        if (selectedWeapon == Sword)
        {
            Rifle.SetActive(false);
            Pistol.SetActive(false);
            Minigun.SetActive(false);
            ToxinLauncher.SetActive(false);
            Sword.SetActive(true);
            RailGunPistol.SetActive(false);
            GravityGuantlet.SetActive(false);
        }

        if (selectedWeapon == RailGunPistol)
        {
            Rifle.SetActive(false);
            Pistol.SetActive(false);
            Minigun.SetActive(false);
            ToxinLauncher.SetActive(false);
            Sword.SetActive(false);
            RailGunPistol.SetActive(true);
            GravityGuantlet.SetActive(false);
        }

        if (selectedWeapon == GravityGuantlet)
        {
            Rifle.SetActive(false);
            Pistol.SetActive(false);
            Minigun.SetActive(false);
            ToxinLauncher.SetActive(false);
            Sword.SetActive(false);
            RailGunPistol.SetActive(false);
            GravityGuantlet.SetActive(true);
        }
    }

    public void UseUtility()
    {
        if (utilityItem == impactGrenade)
        {

        }

        if (utilityItem == impluseGrenade)
        {

        }

        if (utilityItem == repairKit)
        {

        }

        if (utilityItem == advancedRepairKit)
        {

        }

        if (utilityItem == empGrenade)
        {

        }

        if (utilityItem == jetPack)
        {
            
        }
    }
}

…multiple scripts would help with modularity, not hinder it.

2 Likes

You say it works well except for pickups and weapon selection, but that’s all your script does … I guess any day is a good day when you don’t get errors?

You’ll need to change OnTriggerEnter to OnTriggerStay for your pickups to have any chance of working, and I’m not sure why you are assigning gameobjects to bools for weapons.

You’ll have to explain to me what your thinking behind the code was if I’m to be able to give any more insight.

I’m quite new to game development and I didn’t even realize I did that, I managed to fix the script after I looked though some different posts on some reddits and forums and got some help from a friend.

Current Script:

using UnityEngine;

public class PlayerSystems : MonoBehaviour
{
    [Header("HUD")]
    public GameObject pickupText;

    [Header("Weapons")]
    public GameObject Rifle;
    public GameObject Pistol;
    public GameObject Minigun;
    public GameObject Sword;
    public GameObject ToxinLauncher;
    public GameObject GravityGuantlet;
    public GameObject RailGunPistol;

    [Header("Utility Items")]
    public GameObject impluseGrenade;
    public GameObject repairKit;
    public GameObject advancedRepairKit;
    public GameObject grapple;
    public GameObject impactGrenade;
    public GameObject empGrenade;
    public GameObject jetPack;

    [Header("Abilities")]
    public DashAbility dashAbility;
    public HackAbility hackAbility;

    private GameObject curMWep;
    private GameObject curSWep;
    private GameObject selectedWeapon;

    private GameObject selectedUtility;

    void Start()
    {
        curMWep = Rifle;
        curSWep = Pistol;
        selectedWeapon = curMWep;
        ActivateWeapon(selectedWeapon);

        pickupText.SetActive(false);
    }

    void Update()
    {
        HandleInput();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (IsPickup(other.tag))
        {
            pickupText.SetActive(true);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (IsPickup(other.tag))
        {
            pickupText.SetActive(false);
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (Input.GetKeyDown(KeyCode.F) && IsPickup(other.tag))
        {
            HandlePickup(other.tag);
            Destroy(other.gameObject);
        }
    }

    private void HandleInput()
    {
        // Weapon Selection
        if (Input.GetKeyDown(KeyCode.Alpha1)) selectedWeapon = curMWep;
        if (Input.GetKeyDown(KeyCode.Alpha2)) selectedWeapon = curSWep;
        ActivateWeapon(selectedWeapon);

        // Utility Item Usage
        if (Input.GetKeyDown(KeyCode.R) && selectedUtility != null)
        {
            selectedUtility.GetComponent<IUtility>().Use();
        }

        // Abilities
        if (Input.GetKeyDown(KeyCode.E)) dashAbility.Use();
        if (Input.GetKeyDown(KeyCode.Q)) hackAbility.Use();
    }

    private void HandlePickup(string tag)
    {
        GameObject weapon = GetWeaponByTag(tag);
        if (weapon == null) return;

        if (selectedWeapon == curMWep)
        {
            curMWep = weapon;
        }
        else if (selectedWeapon == curSWep)
        {
            curSWep = weapon;
        }

        selectedWeapon = weapon;
        ActivateWeapon(selectedWeapon);
        Debug.Log($"Player picked up and equipped {weapon.name}!");
    }

    private GameObject GetWeaponByTag(string tag)
    {
        return tag switch
        {
            "RiflePickup" => Rifle,
            "PistolPickup" => Pistol,
            "MinigunPickup" => Minigun,
            "SwordPickup" => Sword,
            "RailgunPickup" => RailGunPistol,
            "ToxinPickup" => ToxinLauncher,
            "GravityGunPickup" => GravityGuantlet,
            _ => null
        };
    }

    private void ActivateWeapon(GameObject weapon)
    {
        Rifle.SetActive(weapon == Rifle);
        Pistol.SetActive(weapon == Pistol);
        Minigun.SetActive(weapon == Minigun);
        Sword.SetActive(weapon == Sword);
        RailGunPistol.SetActive(weapon == RailGunPistol);
        ToxinLauncher.SetActive(weapon == ToxinLauncher);
        GravityGuantlet.SetActive(weapon == GravityGuantlet);
    }

    private bool IsPickup(string tag)
    {
        return tag.EndsWith("Pickup");
    }
}

1 Like