I am a high school senior enrolled in a STEM class and I am currently working on a bowling game. I am attempting to input code that will score the 10 pins (0-10) once they are knocked down by the ball or other pins. How would I go about doing so. Note: I am new to the game design scene and have little knowledge of code.
The first thing you need is a reference to the pins. I would probably use a static list for that - put a Pin script on every pin, and this’ll make it easy (and efficient) to find all pins in the scene.
using System.Collections.Generic; //at the top of your script
...
public static List<Pin> all {
get {
if (_all == null) _all = new List<Pin>();
return _all;
}
}
private static List<YourClassName> _all;
void OnEnable() {
if (_all == null) _all = new List<Pin>();
_all.Add(this);
}
void OnDisable() {
_all.Remove(this);
}
Once that exists, the next step is to see which ones are knocked down. That’s pretty simple - just check to see if transform.up is facing up! Vector3.Angle is pretty good for this.
Now you just combine these two concepts with a for loop that counts up.
int CountPinsStillUp() {
int pinsUp = 0;
foreach (Pin pin in Pin.all) {
if (Vector3.Angle (pin.transform.up, Vector3.up) > 10f) {
pinsUp++;
}
}
return pinsUp;
}
Thank you! I appreciate your help and I will test it out.