#weapon spawn/equip/swap not sure
1 messages · Page 1 of 1 (latest)
yep
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
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
just spawn with Instantiate method
what i need from it would be the guntype wich is handthrow or grenadelauncher or whatever
and the ammo[] inside it
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
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))
{
}
}```
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
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
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
this doesnt really make sense, unless you have a ton of game objects with this script on every single one of them. plus i dont see instantiate anywhere in there
u can use struct there, of course, it was just a quick version
i am not really instantiating anything,i do not want to create gameobjects, the way i was trying to do this was by generating the item data , the type of gun and ammo inside it and then copy it to the player
yes, then you should change prefab's child components
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
what do you mean by this ?
you wanna change components in the prefab
If you dont create a game object what is your item spawner doing lol
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
i am not sure myself 🤣 this is the issue
Just curious how do you store "which weapon to spawn"? Is it a reference to the prefab itself
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
[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;
}
Yeah the pickup script itself has a reference to the original weapon prefab