- Home /
How to get the central position of where an object rests on the floor(object)
I am trying to get the position at the centre of a main game object on the side that it touches the floor game object when it is clicked.
I have tried to utilise the onCollision methods. But they only respond when a collision is triggered or ended. The main game object remains on the floor object and does not end or start a new collision. But I need to obtain the position when triggered by a user click. Any suggestions please?
Have you tried using OnCollisionStay? http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionStay.html
Answer by robertbu · Aug 02, 2014 at 06:53 PM
I feel we are missing details critical to giving you an accurate answer. Here are a few possibilities.
If the object always remains upright and you know the size, you can just do:
var posAtFloor = object.transform.position + Vector3.down * distToFloor;
Or you can place an empty game object on the bottom of your object and use that object to get the position on the floor.
If the object always remains upright and you don't know the size, you an get the Mesh.bounds and get the 'distToFloor' from that.
If the object can rotate to arbitrary angles, you can Raycast() down from the pivot point. Note you should use Collider.Raycast() using the collider of the floor, not Physics.Raycast().
Regardless of your comments, you should still be getting collisions from the object. See OnCollisionStay().
Note that 'position on the floor may be a local rather than a world position. If so, take the position generated from any of the above methods and use Transform.InverseTransformPoint().