problem with OnCollisionEnter & Exit.
for some reason the debug code never activates:
using UnityEngine;
using System.Collections;
public class player_movement : MonoBehaviour {
private Rigidbody2D player;
private bool touching;
void Start()
{
player = GetComponent<Rigidbody2D>();
touching = true;
}
void OnCollisionEnter()
{
touching = true;
Debug.Log("enter");
}
void OnCollisionExit()
{
touching = false;
Debug.Log("exit");
}
void FixedUpdate ()
{
Debug.Log(Time.fixedTime + " " + touching);
if (Input.GetButtonDown("Jump") && touching == true)
{
player.AddForce(new Vector2(0,15000));
}
}
}
all it ever displays is true. i made sure that both objects have 2Drigidbodys and colliders but it still doesn't work.
Answer by felixpk · Jul 23, 2016 at 01:54 PM
First of all make sure
You have a collider on both objects that should trigger the collissionEvent
At least one of them has a rigidbody attached
And declare the methods as follow
public void OnCollisionEnter(Collision collision) {
touching = true;
Debug.Log("Enter");
}
public void OnCollisionExit(Collision collision) {
touching = false;
Debug.Log("Exit");
}
Felix
i made sure all of the above were done but it still doesn't work. :( but still thankyou for trying to help!
Your answer
Follow this Question
Related Questions
OnCollisionEnter Problem 1 Answer
OnCollisionEnter - Enabled C# 1 Answer
Problem with gravity, OnCollisionEnter and OnCollisionExit 2 Answers
How can I make a collider that gives me stat up to stop giving me it? 0 Answers
How can i store all colliders of colliders which collide with a prefab? 1 Answer