- Home /
Replacing Weapon 2D Shooter Unity
I am working on a system where you can go up to walls and buy weapons off the wall, similar to CoD Zombies. I can't find anything on picking up weapons in 2D however. I've come up with a system however and it needs some finishing touches.
This is how I've structured my weapon system since I want the player to have a max of two weapons. I just need a way when the player buys a new gun for that prefab to instantiate as child of primary or secondary, depending on which is active, and to destroy the existing child.
Here's the code I've come up with so far, NOTE it currently can only instantiate as a child of the primary with this format. Any help please. For me and the community!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponBuy : MonoBehaviour
{
public GameObject OpenPanel = null;
public WeaponSwitch weapon_switch;
public GameObject weapon1;
public GameObject parentObject;
public Transform Spawnpoint;
void Start ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
OpenPanel.SetActive(true);
}
}
void OnTriggerExit2D(Collider2D other)
{
OpenPanel.SetActive(false);
}
private bool IsOpenPanelActive
{
get
{
return OpenPanel.activeInHierarchy;
}
}
void Update()
{
if (IsOpenPanelActive)
{
if (Input.GetKeyDown(KeyCode.F))
{
OpenPanel.SetActive(false);
Debug.Log("Weapon Bought");
GameObject childObject = Instantiate (weapon1, Spawnpoint.position, Spawnpoint.rotation) as GameObject;
childObject.transform.parent = parentObject.transform;
}
}
}
}
Answer by SalDaDoor · May 30, 2021 at 05:28 PM
The Code that I wrote that now works as intended. Only I am trying to deduct points, which the script is, but if the player doesn't have enough points it will simply deduct the points into the negative and not give the player a weapon. Any tips on this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponBuy : MonoBehaviour
{
public GameObject weapon1;
public GameObject parentPrimary;
public GameObject parentSecondary;
public GameObject primary;
public GameObject secondary;
public GameObject OpenPanel = null;
public int amountToPurchase;
public Transform Spawnpoint;
public GameObject player;
public float pointsToTake;
private float points;
void Start()
{
points = player.GetComponent<TopDownMovement>().points;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
OpenPanel.SetActive(true);
}
}
void OnTriggerExit2D(Collider2D other)
{
OpenPanel.SetActive(false);
}
private bool IsOpenPanelActive
{
get
{
return OpenPanel.activeInHierarchy;
}
}
void Update()
{
if (IsOpenPanelActive)
{
if (Input.GetKeyDown(KeyCode.F))
{
OpenPanel.SetActive(false);
player.GetComponent<TopDownMovement>().points -= pointsToTake;
Debug.Log("Weapon Bought");
if(points >= amountToPurchase)
{
if (primary.activeSelf)
{
Destroy(primary.transform.GetChild(0).gameObject);
GameObject childObject = Instantiate(weapon1, Spawnpoint.position, Spawnpoint.rotation) as GameObject;
childObject.transform.parent = parentPrimary.transform;
}
if (secondary.activeSelf)
{
Destroy(secondary.transform.GetChild(0).gameObject);
GameObject childObject = Instantiate(weapon1, Spawnpoint.position, Spawnpoint.rotation) as GameObject;
childObject.transform.parent = parentSecondary.transform;
}
}
else
{
Debug.Log("Not Enough Points");
}
}
}
}
}