2D Shooter Make Item Pickup Change Bullet Prefab On Collision
I have a bullet script where the rigidbody(rb) is a prefab of one type of bullet, say earth and upon collision of another bullet type say fire, the prefab changes to fire. I have setup tags for each bullet but
if (collision.gameObject.tag == "RedBullet")
{
PlayerShoot.Prefab "Then Item changes the Bullet" == "RedBullet";
}
I don't know how your bullet system is setup but instead of doing this, you need to create all the different type of bullets as children of the player and set the desired bullet as the active ine when required, and create a database within the script to manage the different bullets and swap them out whenever you want.....an array is really good for this
Answer by codedbythet · Aug 07, 2021 at 02:57 AM
how do you tell the array to make the bullet active?
Just run a simple for loop with a simple if tag which checks if the index of the current bullet matches the desired bullet, if yes, set it active and set it as the current bullet, else disable it with the SetActive statements...let me give you an example on how it can work, (you would have to modify it to fit your code ofcourse, but shouldn't be much)
using UnityEngine;
using System;
public class BulletSwitch: MonoBehaviour
{
public GameObject[] bullets;
public GameObject currentBullet;
public int currentBulletIndex;
public PlayerShoot playerShoot;
void Update()
{
if(Input.GetKeyDown(Keycode.Q))
{
currentBulletIndex ++;
if(currentBulletIndex >= bullets.Length)
{
currentBulletIndex = 0;
}
SetBullet();
}
else if(Input.GetKeyDown(KeyCode.E))
{
currentBulletIndex --;
if(currentBulletIndex < 0)
{
currentBulletIndex = bullets.Length - 1;
}
SetBullet();
}
}
public void SetBullet()
{
currentBullet = bullets[currentBulletIndex];
playerShoot.bullet = currentBullet;
for(int i=0; i < bullets.Length; i++)
{
if(i == currentBulletIndex)
{
bullets[i].SetActive(true);
}
else
{
bullets[i].SetActive(false);
}
}
}
}
Change the name of PlayerShoot to whatever class your PlayerShoot script is of and change the name of playerShoot.bullet to whatever the name of the bullet is in your script....and then assign this script to the player in inspector....then make all the bullet prefabs children of the player....and assign them to the bullets array in the inspector...This should do what you intend.....hope this helps
This has been very helpful. So I am trying to build a c# that attaches to the missile that when the collider is activated the player shoot script bullet prefab object changes.
void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Player")) {
I tried your script and I can get it to recognize the collision, but it instead of selecting the new bullet prefab, it only changes to the current one.
Your answer
Follow this Question
Related Questions
Cant figure out how to make a pickup that swaps one game object for another. 0 Answers
Player trigger on platform with prefab 0 Answers
How do I make item pickup change sprite on collision? 1 Answer
Picking up item if player is within a certain radius of the item. 0 Answers
why isnt my code working, 2 Answers