- Home /
Question by
SUPPEAR · Feb 05, 2015 at 08:53 PM ·
androidgameobject
Make object kinematic when grounded?
Hello, I am fairly new to Scripting/Game making. I have been trying to figure out how to make my objects kinematic ONLY when it is on the ground or on top of another object. If anyone knows how to do so all help is very appreciated. Note: This is a 2D game.
Comment
Not quite sure if that'd be right or not, pretty confused by it also. The objects are squared and I don't want them to be able to move when it is on top of the ground/objects.
doenst matter if the object it square or not, and i forgotten the child collider must be trigger. if you want to check with physics2D.overlapArea, search in the scripting documentation.
check the edit, i forgotten to add the sentence of the rigidbody2d
Answer by ReContraChanfle · Feb 05, 2015 at 09:19 PM
public LayerMask ground; // assign in the inspector, the layers that you want to check as "ground" or "top object"
bool isGrounded; // this will go true when gorunded, flase when not.
float radius; // this is the radius of the circle collider that you will assign to the gameobject in question, put in the botton or where you want to check the collision.
Transform checkGround; // this is the transform of the child you will assign to the main object, you will add a circlecolllider2d component to it, and assing the radius that you think properly to check the collision.
void Start()
{
checkGround = gameObject.Find("check").GetComponent<Transform>();
radius = gameObject.Find("checkGround1").GetComponent<CircleCollider2D> ().radius;
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle (checkGround.position,radius,ground);
if(isGrounded)
gameobject.rigidbody2D.isKinematic = true;
Debug.Log(isGrounded);
}