Hi friends, I have a simple flicking game in which I get one point if object has landed.
Can someone please advise on how I can:
- check if object is upside down
- award 5 points if upside down
everything works as it should when upright but I would like to add extra points if object lands upside down.
thanks to all for your help and happy new year to you all!!
Quite simple way. Create two empty objects and put them into object you are flipping hierarchy wise.
- One at the top of the object you are flicking
- Second at the bottom of the object you are flicking
FlickingObject (lets say it is 1x1x1 cube)
Top (localPosition = (0.0, 0.5, 0.0))
Bottom (localPosition = (0.0, -0.5, 0.0))
Than when object lands you can do simple position comparing:
if (Mathf.Abs(top.position.y - bottom.position.y) < 0.001f)
Debug.Log("Landed on the side");
else if (top.position.y > bottom.position.y)
Debug.Log("Landed with top looking up");
Debug.Log("Landed with bottom looking up");
P.S. Feel free to remove first checking if it is not possible for your object to land neither on top neither on bottom. Like coin landing on it’s edge.
Do you have a 2D game or 3D?
Well… I would say that the easiest way is to check rotation in a moment of collision. For example, if you have a 2D game. You should decide between what range the object will be considered as upside down. Then,
float angle = obj.rotation.eulerAngles.z;
if(angle > 90f && angle < 270f)
// Upside down -> Add more points
For 3D game is mostly the same, but you should check 2 axis: x and z.
thanks guys, its is a 2d game, I was going along the lines of if (Vector3.Dot(transform.up, Vector3.down) > 0)
but unfortunately I am far from a coder, just trying to learn it and understand it all.
Thanks I will try your suggestions and post back