Unity bowling pins detection

I'am working on bowling ball game and I want to detect how many bowling pins has been knocked down by ball? What is the solution for this problem?

Answers 2

  • Depending on precise requirements, the detection can be as simple as checking the transform.position.y:

    1. (optional) add child Top at top of each bowling pin
    2. When placed, record the vertical position of (Top of) pin
    3. each Update() check if pin's original and current vertical position difference is greater than a threshold (e.g. 1/2 pin height)

      bool isKnocked = Mathf.Abs(transform.position.y - originalY) > halfHeightThreshold;

    4. (optional) when crossed the threshold, emit PinKnocked, a custom UnityEvent<Pin> which you have subscribed to in your game logic controller (e.g. GameManager)

      if(isKnocked && !wasKnockedLastFrame) OnKnocked?.Invoke(this);


  • Here's an example similar to how I detected domino tipping in Last One Standing.

    public class Tippable : MonoBehaviour {
    
        // roughly 1/sqrt(2), for a 45 degree angle.
        const float TIP_THRESHOLD = 0.707f;
    
        // This lets you register another script to
        // listen for "tip" events & tally them.
        public UnityEvent onTip;
    
        public bool tipped {get; private set}
    
        // Start with a loop to check if we're tipped over.
        IEnumerator Start() {
    
            while(!tipped) {
                // Wait till next frame.
                yield return null;
    
                // Check if our local up is not very upward anymore.
                tipped = transform.up.y < TIP_THRESHOLD;
            }
    
            // Report that we've tipped.
            onTip.Invoke();
        }
    }
    

Related Questions