- Home /
Finding number of bowling pins knocked over
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;
}
}
How does your rotation test work?
I'd probably find the angle between the pin's axis and the vertical - and if that's more than some threshold then the pin's down. This way you have a tolerance parameter that you can define in degrees so it's all a bit more fathomable.
I might also have a collider, but not in the way that answers are suggesting... I'd just have one covering the lane so that I could detect when a pin's been knocked clean away (and then, only because such a pin could in theory land upright and be missed by the angle test). But I doubt if I'd actually use physics collisions with that. I'd be waiting for the pins to stop moving before doing any of this, so could just check to see if they're within the lane collider at that point.
Answer by Defetysta · Nov 23, 2018 at 01:00 PM
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.
You are making good points, but still I believe that, with little change, that solution would be easier. You could make a trigger on a pin that starts in the widest point of the pin and goes up to the top of it, and after the round ends check whether the trigger overlaps anything.
I can't see how that could ever be thought easier than "calculate the angle from the vertical and see if it is less than some threshold"
Answer by mlnczk · Nov 23, 2018 at 01:03 PM
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.
Then add boolean inside OnTriggerEnter that it touched the ground this instance. And reset it each time we reset pins positions
Not sure how that address my question (or what you mean by "...this instance...").
And what happens if a pin is knocked over but ends up resting on some other pins? Or gets knocked a long way.
Of course, these things are all fixable, with more colliders, more flags and so on. But it's all a lot of work to do something that ought to be straightforward.
So what advantage do you think your approach has over that of the OP?
Answer by s_awali · Nov 23, 2018 at 01:02 PM
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?
In that case, you can create 3 empty game objects at the base of your pin, placed evenly. When you want to know if the pin has fall, do 3 Physics.OverlapSphere at the 3 positions. If your floor gameobject is not in the 3 OverlapSphere, it means the pin bottom surface is not flat on it, meaning the pin has fallen.
Quick code example:
public Transform FirstContactPoint;
public Transform SecondContactPoint;
public Transform ThirdContactPoint;
private bool HasFall()
{
int numberOfContact = 0;
float radius = 0.2f;
if (Physics.OverlapSphere(FirstContactPoint.position, radius).Where(x => x.tag == "Floor").FirstOrDefault() != null)
numberOfContact++;
if (Physics.OverlapSphere(SecondContactPoint.position, radius).Where(x => x.tag == "Floor").FirstOrDefault() != null)
numberOfContact++;
if (Physics.OverlapSphere(ThirdContactPoint.position, radius).Where(x => x.tag == "Floor").FirstOrDefault() != null)
numberOfContact++;
return numberOfContact != 3;
}
This will require "using System.Linq;"
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Ideas for detecting collision speed with trigger? 2 Answers
why won't my script do a proper rotation check 1 Answer
Collision Not Working 1 Answer