slaga
February 19, 2021, 3:18pm
1
i have this function for setting bullet types through an enum.
public void SetBulletType(BulletTypes type)
{
sprite.sprite = bulletData[(int)type].sprite;
cc2d.radius = bulletData[(int)type].radius;
transform.localScale = bulletData[(int)type].scale;
}
and i have this function in my player controller for shooting the bullets
bullet.GetComponent<BulletScript>().SetBulletType(BulletScript.BulletTypes.Default);
how can i create a function that changes the bullet type with a keypress??
Look up Unity UI tutorials for connecting functions to buttons, then connect appropriately.
slaga
February 19, 2021, 3:33pm
3
oh i meant button press! not ui buttons! like cycle through the enum on keypress
slaga
February 19, 2021, 3:35pm
4
i want to make a weapon system and since my bullet script has a few different types of bullets , at least colors and sprites i was wondering if i could change the type with a keypress in the game.
public enum BulletTypes { Default, MiniBlue, MiniGreen, MiniOrange, MiniPink, MiniRed ,Charged};
[SerializeField] BulletTypes bulletType = BulletTypes.Default;
slaga
February 19, 2021, 3:36pm
6
yeah thanks but again not what i asked! or maybe i asked wrong! sorry if thats the case. just how to cycle through the enum with getkeydown!
If the enums are sequential, just increment them as if they’re an integer.
When they reach the last one, cycle it back to the first.
If the enums are not sequential, put them into an array, keep an index, cycle the index through the array.
I usually write all my enums like this:
public enum BulletType
{
BB,
Pistol,
Rifle,
Shotgun,
// insert all new items here
MAXIMUM,
};
Then I know what the last one is.
private BulletType bulletType;
to increment:
bulletType++;
// wrap?
if (bulletType >= BulletType.MAXIMUM)
{
bulletType = 0;
}
slaga
February 19, 2021, 4:24pm
10
wow great! i though of doing a for loop but this one is just the thing i needed!