- Home /
How can i detect which side of a box collider 2D the collider object collided with?
I have 2 gameobjects each with a box collider2d and rigidbody2d attached to them. If the first gameobject(player) collides with the second gameobject i would like to know on which side was the collision on the second object so i can move the second object in some direction.
Thanks in advance.
Answer by Spartiate0033 · Mar 29, 2016 at 05:36 AM
You may want to consider creating child objects of the Player and both of these child objects would have box collidors on them set to triggers. You'd have the left child object and the right child object. Both objects would have a script attached and they would simply contain a OnTriggerEnter() function. When triggered it would fire a message to the player script. I recommend having the player script a reference in the child object scripts for quick and efficient access.
Answer by Addyarb · Jun 13, 2015 at 02:54 PM
Raycasting when an object is hit is the way I would approach that. You could also use triangle indexes, but that could be a little complicated, and inefficient if you're just using a true 6-sided cube.
Here's a little example script.
EDIT: This works for 3D, but I'm sure you could edit it to work for 2D as well. Just take out the forward and back block bits.
Good luck!
public Transform blockAbove;
public Transform blockBelow;
public Transform blockRight;
public Transform blockLeft;
public Transform blockForward;
public Transform blockBack;
void OnTriggerEnter(){
CheckSurroundingBlocks();
}
void CheckSurroundingBlocks(){
//First let us raycast upwards to check for block above us...
Vector3 up = transform.TransformDirection (Vector3.up);
RaycastHit hitUp;
//If we cast a ray up and hit something...(we ray cast twice this object's extents, or size of the object).
if (Physics.Raycast (transform.position, up, out hitUp, GetComponent<Renderer> ().bounds.extents.x*2)) {
//the block above us is that block and that block's tag is "BlockTag"...
if(hitUp.collider.tag == "BlockTag")
blockAbove = hitUp.transform;
}
//repeat for the other directions..
Vector3 down = transform.TransformDirection (Vector3.down);
RaycastHit hitDown;
if (Physics.Raycast (transform.position, down, out hitDown, GetComponent<Renderer> ().bounds.extents.x*2)) {
if(hitUp.collider.tag == "BlockTag")
blockBelow = hitDown.transform;
}
Vector3 left = transform.TransformDirection (Vector3.left);
RaycastHit hitLeft;
if (Physics.Raycast (transform.position, left, out hitLeft, GetComponent<Renderer> ().bounds.extents.x*2)) {
if(hitUp.collider.tag == "BlockTag")
blockLeft = hitLeft.transform;
}
Vector3 right = transform.TransformDirection (Vector3.right);
RaycastHit hitRight;
if (Physics.Raycast (transform.position, right, out hitRight, GetComponent<Renderer> ().bounds.extents.x*2)) {
if(hitUp.collider.tag == "BlockTag")
blockRight = hitRight.transform;
}
Vector3 ahead = transform.TransformDirection (Vector3.forward);
RaycastHit hitAhead;
if (Physics.Raycast (transform.position, ahead, out hitAhead, GetComponent<Renderer> ().bounds.extents.x*2)) {
if(hitUp.collider.tag == "BlockTag")
blockForward = hitAhead.transform;
}
Vector3 back = transform.TransformDirection (Vector3.back);
RaycastHit hitBack;
if (Physics.Raycast (transform.position, back, out hitBack, GetComponent<Renderer> ().bounds.extents.x*2)) {
if(hitUp.collider.tag == "BlockTag")
blockBack = hitBack.transform;
}
}
Your answer
Follow this Question
Related Questions
How to check if BoxCollider2D collided with another BoxCollider2D? 2 Answers
Player sprite clipping through game boundary in 2d scene 1 Answer
Coin collection script (not working) 1 Answer
Object passes through collider even when isTrigger is turned off. 2 Answers
Particles only colliding with one of the player's two colliders? 0 Answers