0
\$\begingroup\$

I have a small problem and it would be very helpful, which could guide me. What happens is the following, I have an enemy (evil usb) in my scene, to the collisionar this enemy with my main player threw the enemy always right falling down the map. All right up here, what I need now is to know that exact point I'm colliding my enemy to know if I should throw it to the right or left depending on the point of collision which can be at the Centre to the right or to the left of my enemy like this imagen:

enter image description here

I read something about contact point, but can not understand how it works and really serves me, here is the code that I have to collision and throw it to the right:

void OnCollisionEnter(Collision other){

    if (other.gameObject.CompareTag ("Inacapin")) {
       enemyrigid.AddForce (-transform.right * 500, ForceMode.Acceleration); // work fine

      /*if(point of collision right){ // I need something like that

            enemyrigid.AddForce(-transform.right * 500, ForceMode.Acceleration);

         }else if(point of collision left){
            enemyrigid.AddForce(-transform.left* 500, ForceMode.Acceleration);
            }else{
             enemyrigid.AddForce(transform.forward* 500, ForceMode.Acceleration);
          }         

     }

  }

hope they can help me, thank you... ;)

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You should use the member contacts from the Collision object other.

This is the example in the API docs:

void OnCollisionStay(Collision collision) {
    foreach (ContactPoint contact in collision.contacts) {
        print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
        Debug.DrawRay(contact.point, contact.normal, Color.white);
     }
}

Hope it helps.

\$\endgroup\$
2
  • \$\begingroup\$ Okey but how put something like this in my if validation , I don't understand how work contact.point can you explain me? please ;) \$\endgroup\$ Sep 7, 2018 at 16:46
  • \$\begingroup\$ Well, the ContactPoint class has a Point inside, which is a Vector3, just use that position for your comparison \$\endgroup\$ Sep 7, 2018 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .