- Home /
 
How to trigger different animations depending on where a gameobject collides?
I am making a third person game where the player is allowed to pick up and throw a ball. I am trying to make so that if the player collides with the ball below his torso, a bend over and pick up animation triggers. and if the ball collides with the player above his torso a catch animation triggers. Here is my script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playercontroller : MonoBehaviour {
          static Animator anim;
          public float movementSpeed = 10.0F;
          public float rotateSpeed = 100.0F;
          public GameObject carryact;
          public Transform ball;
          public GameObject cachact;
          // Use this for initialization
          void Start ()
          {
              anim = GetComponent<Animator>();
          }
          // Update is called once per frame
          void FixedUpdate ()
          {
               ControllPlayer();
             if(carryact.activeInHierarchy)
             {
                 if (Input.GetMouseButtonDown(0))
                      {
                              anim.SetTrigger("isThrowing");
                      }
             }
          if (Input.GetButton("Horizontal"))
              {
                  anim.SetBool("isRunning",true);
                  anim.SetBool("isIdle",false);
              }
              else
              {
                  anim.SetBool("isRunning",false);
                  anim.SetBool("isIdle",true);
              }
              if (Input.GetButton("Vertical"))
              {
                  anim.SetBool("isRunning",true);
                  anim.SetBool("isIdle",false);
              }
          }
          void OnCollisionEnter (Collision col)
           {
                   if(col.gameObject.name == "ball")
                   {
                      anim.SetTrigger("isGrabbing");
                   }
           }
          public Transform CameraTransform ; // Drag & Drop the camera in the inspector
           void ControllPlayer()
           {
               float moveHorizontal = Input.GetAxisRaw ("Horizontal");
               float moveVertical   = Input.GetAxisRaw ("Vertical");
               Vector3 right        = Vector3.ProjectOnPlane( CameraTransform.right, Vector3.up ) ;
               Vector3 forward      = Vector3.ProjectOnPlane( ( Vector3.Dot( -CameraTransform.forward, Vector3.up ) > 0.99f ) ? CameraTransform.up : CameraTransform.forward, Vector3.up ) ;
               Vector3 movement = right.normalized * moveHorizontal + forward.normalized * moveVertical;
               if (movement != Vector3.zero)
               {
                   transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.normalized), 0.2f);
               }
               transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);
           }
 
               }
Answer by Ymrasu · Jan 29, 2019 at 05:04 PM
In OnCollisionEnter you can use the Collision object to find where the collision occurred with col.GetContact(0).point
It is in world space though, so you would need to subtract your player's position to get a 'local' position. Then you can check the y position to see if it is above or below your torso.
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.name == "ball") {
             // turn to local position
             Vector3 localContactPoint = col.GetContact(0).point - transform.position;
 
             // Assuming player position is at feet of player and player is 2 units tall
             if (localContactPoint.y > 1f) { // above waist
                 Debug.Log("Catch");
                 // anim.SetTrigger("CatchBallAnim");
             } else { // else below waist
                 Debug.Log("Pick Up");
                 // anim.SetTrigger("PickUpAnim");
             }
         }
     }
 
              I put the code in and it says that unityengine.collision does not have a definition for GetContact. is there a fix for that?
Do you have another script you made called Collision? If you do it could be interfering.
No. It also says am I missing an assembly reference
Your answer