- Home /
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");
}
}
}
The object that is hitting the "Platform01" has to exit the collision area of the Platform01 and enter again in order to trigger again the event OnCollisionEnter2D.
Are you sure you are using a rigidbody2d component and Collider2d components in both gameobjects (this game object and the "platform01").
Is your rigidbody2d platform kinematic? If not, consider to make it kinematic. $$anonymous$$ark your Platform01 GameObject as static, and put gravityscale in 0 value. From the editor, not from code, freeze all constraints, and don't use rigidbody functions affecting Platform01.
But...
Why are you freezing the rigidbody2d of the platform01 inside OnCollisionEnter2D?
I want to freeze the position on the platform to prevent lots of shaky movement to the player while the platform moves. It works, the debug only ever outputs "false," I assume because it only checks once and the player starts off the platform. I'm not using $$anonymous$$inematic but even if I do check it on the platform it doesn't seem to make a difference anywhere. Both the platform and the player have rigid bodies and colliders.
Answer by Measurity · Dec 27, 2015 at 12:59 AM
I think you want to change OnCollisionEnter2D to OnCollisionStay2D:
Or set the anim.SetBool("istouch") to true in:
OnCollisionEnter2D and false in: OnCollisionExit2D
Answer by Kiwasi · Dec 27, 2015 at 12:50 AM
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.
Answer by PabloUnityArgentina · Dec 27, 2015 at 12:59 AM
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?