Collision checked once, then never again. How can I make it always check if its touching? C#

I have a function that checks for collisions, I was hoping I could have it change the animation when it sees that I’m not touching or am touching an object. The problem is that it only seems to check the instant I hit play and never again, so I can’t change the animation. Is there a way to fix that? The function that is the problem is the last one.


using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
public float speed = 1f;
public float jumpSpeed;
public Rigidbody2D rb;
Animator anim;

void Start () {
	anim = GetComponent<Animator>();
	rb = GetComponent<Rigidbody2D>();
}

void Update () {
	if (Input.GetKey (KeyCode.D) && (transform.position.x < 3.5)) {
		transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0f);
		transform.localScale = new Vector3 (2.25f, 2.25f, 0f);
	}
	
	if (Input.GetKey (KeyCode.A) && (transform.position.x > -3.5)) {
		transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0f);
		transform.localScale = new Vector3 (-2.25f, 2.25f, 0f);
	}
	
	if (Input.GetKeyDown (KeyCode.S)) {
		gameObject.GetComponent<Rigidbody2D> ().gravityScale += 1;
	}
	
	if (Input.GetKeyDown (KeyCode.W)) {
		gameObject.GetComponent<Rigidbody2D> ().gravityScale -= 1;
	}

	if (Input.GetKeyDown (KeyCode.Space)) {
		rb.AddForce(transform.up * jumpSpeed);
	}
}

void OnCollisionEnter2D(Collision2D col) {
	if (col.gameObject.name == "Platform01") {
		gameObject.GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.FreezePosition;
		anim.SetBool ("istouch", true);
		Debug.Log ("True");
	} else {
		anim.SetBool("istouch", false);
		Debug.Log("False");
	}
}

}

I think you want to change OnCollisionEnter2D to OnCollisionStay2D:

Or set the anim.SetBool(“istouch”) to true in:
OnCollisionEnter2D
and false in: OnCollisionExit2D

Sounds like you want OnCollisionStay instead of OnCollisionEnter. Be warned, lots of OnTriggerStay calls will kill performance.

The other option is to track objects in OnColissionEnter and OnCollisionExit. Use a list or a bool to keep track of the state and respond appropriately.

The object that is touching the “Platform01” has to exit the “collider zone” and enter again in order to trigger the OnCollisionEnter2D function again.

Are you sure you have rigidbodies2D an Colliders2D for those two game objects?

Is the rigidbody from Platform1 Kinematic?

Why are you freezing the position of that rigidbody inside the OnCollisionEnter function?