Files
OnlyNPC/Assets/FPS/Scripts/Gameplay/WeaponPickup.cs

42 lines
1.3 KiB
C#
Raw Normal View History

2026-05-26 09:46:57 +07:00
using Unity.FPS.Game;
using UnityEngine;
namespace Unity.FPS.Gameplay
{
public class WeaponPickup : Pickup
{
[Tooltip("The prefab for the weapon that will be added to the player on pickup")]
public WeaponController WeaponPrefab;
protected override void Start()
{
base.Start();
// Set all children layers to default (to prefent seeing weapons through meshes)
foreach (Transform t in GetComponentsInChildren<Transform>())
{
if (t != transform)
t.gameObject.layer = 0;
}
}
protected override void OnPicked(PlayerCharacterController byPlayer)
{
PlayerWeaponsManager playerWeaponsManager = byPlayer.GetComponent<PlayerWeaponsManager>();
if (playerWeaponsManager)
{
if (playerWeaponsManager.AddWeapon(WeaponPrefab))
{
// Handle auto-switching to weapon if no weapons currently
if (playerWeaponsManager.GetActiveWeapon() == null)
{
playerWeaponsManager.SwitchWeapon(true);
}
PlayPickupFeedback();
Destroy(gameObject);
}
}
}
}
}