# My solution for the standing pin problem

I tried this with the challenge, and had some difficulty. The problem was that even a very small negative value, like -0.1, were giving me large rotations of 359.9, which of course is more than the threshold and resulted in a “fallen” pin.
So I watched Ben’s way, copied his solution, and had the same problem. So I made some modifications to the math. Here is my IsStanding() method:

public bool IsStanding(){
Vector3 rotationInEuler = transform.rotation.eulerAngles;
float tiltInX = 180f - Mathf.Abs (180f - rotationInEuler.x);
float tiltinZ = 180f - Mathf.Abs (180f - rotationInEuler.z);
if (tiltInX < standingThreshold && tiltinZ < standingThreshold) {
return true;
}
return false;
}

The difference is in the calculation of tiltInX and tiltInY. The (180 - rotation) gives the angle from the “south pole”, so to speak, and Mathf.Abs ensures that it is represented as a positive value. So 10 degrees will give you a value of 170, and -10 degrees (which is 350 degrees to Unity) will also give you a value of 170. Subtracting this number from 180 gives you 10, which is the number of degrees from 0 (standing).

I checked it out, and using a Boolean in the update method, made it display “1 Pin fell”, “2 Pin fell”, etc. It works when I swipe, and when I manage to knock down a ball or two.

2 Likes

Seems that we are at the same point and I was asking myself the same question when I see a result false because my pin was at 360° instead of 0° …

Thanks for the share

There is also another problem you may come across, due to the fact the pin has been rotated 270 degrees you can run in to a problem known as gimbal lock, so that’s something to watch out for

Yeah ! i’ve made my own pins and my parameters are excellent, 0° rotation on my new pins

nb: The good point on redo the pin gameobject in unity show me that I’ve done my resize in Blender the wrong way… it’s really interesting to have take a look at Blender before begin section 8

That’s exactly what I did lol. Quite handy doing the blender course in parallel xD

Well, once we restructured the pins, and mine were sitting at a 270-degree angle, I had to rethink this. After some trial and error, I finally looked into the dreaded Quaternion. I store the initial rotation into a variable, then in IsStanding() I compare the current rotation with the initial one using Quaternion.Angle. That returns an angle from straight up. As long as this angle is lower than standingThreshold, that boy is standing!
I’m still playing with the threshold, though. Trying to find the middle ground between wobbling forever and calling a standing pin down.

1 Like