#weapon spawn/equip/swap not sure

1 messages · Page 1 of 1 (latest)

exotic patrol
#

like this ?

tawdry brook
#

yep

exotic patrol
#

ok , so the problem is the following , my weapons will shoot with gameobjects , prefabs

#

i want to be able to spawn those weapons inside an item spawner

#

or ammo

#

i am not sure wich i am supposed

cerulean prawn
#

it depends really on what you want, but i think a fake gun prefab would be the best option. You could customize it with how it stays on the floor and add a script to handle whatever variables you need for the gun info

tawdry brook
#

just spawn with Instantiate method

exotic patrol
#

what i need from it would be the guntype wich is handthrow or grenadelauncher or whatever

#

and the ammo[] inside it

cerulean prawn
#

yes, that would be what the script is for on it

#

unless theres a better way to transfer that information, because to my knowledge u would need getComponent for what i am saying

exotic patrol
#

right now in my inventory class im checking the following

 private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.GetComponent<ItemSpawner>() == null) { return; }

        if (Input.GetKeyDown(KeyCode.E)) 
        {
        }

    }```
cerulean prawn
#

otherwise you could drop the real gun, and enable/disable scripts on the gun when its on the floor or player

#

your guns are not a child of ItemSpawner so that doesnt make too much sense

#

it would be of type Gun

exotic patrol
#
    public SpriteRenderer itemspawnsprite;
    public GameObject spawneditem;  //of type gun ???? 
    //public Gun spawneditem;
    public GameObject[] itemtospawnlist;

    public float timetospawn=0,timebetweenspawns=10;

    // Update is called once per frame
    void Update()
    {
        if (spawneditem == null)  
        {
            if(timetospawn < timebetweenspawns)
            {
                timetospawn += Time.deltaTime;
            }
            else 
            {
                Spawnitem();
            }
        }
        
        if (Input.GetKeyDown(KeyCode.O)) { Spawnitem(); } ;
    }

    public void Spawnitem() 
    {
        int randomitemindex = Random.Range(0, itemtospawnlist.Length); //random grenade prefab from list

        spawneditem = itemtospawnlist[randomitemindex];

        itemspawnsprite.sprite = spawneditem.GetComponent<SpriteRenderer>().sprite;
        itemspawnsprite.color = spawneditem.GetComponent<SpriteRenderer>().color;
    }
#

this is my current item spawner code

tawdry brook
#
using UnityEngine;
using System.Collections;

public class SpawnManager : MonoBehaviour
{
    [SerializeField] private GameObject[] weaponPrefabs;
      
    private readonly ((float min, float max) x, (float min, float max) y, (float min, float max) z) spawnPos = ((-1f, 1f), (-1f, 1f), (-1f, -1f));     

    private readonly float spawnDelay = 1f, spawnInterval = 3f;

    private void Start() => StartCoroutine(SpawnWeapons());

    private IEnumerator SpawnWeapons()
    {
        yield return new WaitForSeconds(spawnDelay);

        while (true)
        {
            Instantiate(weaponPrefabs[Random.Range(0, randomPrefabs.Length)], new (Random.Range(spawnPos.x.min, spawnPos.x.max), Random.Range(spawnPos.y.min, spawnPos.y.max), Random.Range(spawnPos.z.min, spawnPos.z.max)), Quaterniaon.identity);              

            yield return new WaitForSeconds(spawnInterval);
        }
    };
}
#

smth like this, or what?

#

I hope there are no compiler errors, cause I have been writing it in discord

cerulean prawn
tawdry brook
exotic patrol
tawdry brook
smoky drum
#

Here's how it works on one of my projects.
Each weapon has an associated pick-up object, with the 3D model of the weapon and a script that dictates which weapon to spawn, the state it's in and its ammo. That allows me to drop a weapon and pick it back up in the exact state it was dropped.
Then, a script on the player is in charge of detecting the pick-ups, and giving them to the inventory management script. From there, it instantiates, enables, disables, and destroys the weapons at will

exotic patrol
tawdry brook
cerulean prawn
tawdry brook
#

if you have a weapong, let it be sword, then this sword should have handle and blade

#

you can then change their color, prefab, collider, rigidbody

#

whatever you want

#

just like

exotic patrol
cerulean prawn
exotic patrol
#

i thought that maybe ill make the spawner create a Gun of random type and asign its ammo and then copy it to the player

tawdry brook
#
[SerializeField] private GameObject sword;

private void Start()
{
    GameObject blade = sword.transform.GetChild(0).gameObject; // the first child that's blade

    blade.GetComponent<Rigidbody>.mass *= 10f;
}
smoky drum