Finding number of bowling pins knocked over (217035)

I'm making a Unity 3D bowling game but haven't figured out how to tally up the number of pins knocked after the ball has hit.

I have tried noticing when the pins rotation has changed but it doesn't seem to work.

I am destroying the ball and that is how the level ends so and instantiates the next level.

Below is the code I added to the pin. Do I need to have an on-collision event for this script to be called. Currently my console just prints "Pin is Standing" the whole time.

public class Pin : MonoBehaviour {

public int score;

public void Update()
{
    TallyScore();
}

public void TallyScore()
{

    //checking if the pin is standing up (if it's not, the pin's rotation will be not 0)
    if (transform.rotation.x < 1 && transform.rotation.z < 1)
    {
        Debug.Log("Pin is standing");
    }
    else
    {
        //pin is knocked over
        score++;
        Debug.Log(score);
    }
}

public int getScore()
{
    return score;
}

}

Hey man, good to hear. Glad i could help. With these types of questions it is not given that the advice actually helps :D

3 Answers

3

To be honest, I think the easiest way to do this would be to attach collider on the top of the head of the pin and one on the ground. Sometimes the pin may tilt but would not get knocked over, so it is not a perfect solution, but when those two collide - you are sure that the pin was knocked over.

Not for me. I think you're making things unnecessarily complicated and that the OP's approach makes a lot more sense. See comments elsewhere.

In the start method of the Pin, save the original position & rotation :

    public Vector3 _originalPosition;
    public Quaternion _originalRotation;
    
	void Start ()
    {
        _originalPosition = transform.position;
        _originalRotation = transform.rotation;
    }

Then, when you want to know if the pin has moved, use this fonction :

    private bool HasMoved()
    {
        return transform.position != _originalPosition || transform.rotation != _originalRotation;
    }

So what if the pins gets shoved slightly to one side? Or falls but bounces back up in a different place?

You can add a colider to the middle or top of your pin prefab. Then check oncollisiontrigger if it hits the ground collider and add it to some global score points controlled by other object

And what if the pin bounces back up? Seems to me that whatever solution one uses, it should wait till they've all stopped moving before checking. And then, simply looking at the angle from the vertical ought to be sufficient.