Detect collision from direction
Hey hows it going,
I am currently working on a 2.5D sidescroller game similiar to geometry dash, and I need to check if the player has collided with the side of an obstacle, but not if the flip onto it or are using it as a platform. I was considering doing a raycast, but because a raycast only goes from one point, so if anything above the point the raycast was sent from collides with the object, it won't detect it.
What is the most efficient way to detect a collision and if it is on the side of the object?
Thanks alot!
I am trying this code here:
void OnCollisionEnter(Collision other){
if(other.gameObject.tag.Equals("Obstacle")){
foreach(ContactPoint point in other.contacts){
if(point.point.x < other.transform.position.x - other.collider.bounds.extents.x -0.1f){
gameObject.SetActive(false);
if(state == gravityState.up){
SwitchGravity();
}
GameObject go = Instantiate(destroyedCube, transform.position,Quaternion.identity) as GameObject;
print(go.transform.childCount);
for(int i = 0; i<go.transform.childCount;i++){
if(go.tag == "broken")
go.transform.GetChild(i).GetComponent<Rigidbody>().velocity = rb.velocity;
}
}
}
}
}
But it doesn't seem to work.
This was my initial idea, and I think the concept is sound, but my execution is wrong, any thoughts?
EDIT: If you add 0.1f ins$$anonymous$$d of taking it, it gives a little bit more room and it works!
Your answer
Follow this Question
Related Questions
How i can fix a player controller bug? 0 Answers
Object reference not set to an instance of an object 0 Answers
Collisions not working in top-down RPG? 0 Answers
What is ContactPoint.point? 0 Answers
Switch Scene on Collision with a Box 1 Answer