- Home /
Unity Trigger 2D not working
Hello All,
My OnTriggerEnter2D does not seem to be registering or working at all. I am making my first mobile game ad am dragging my player around the screen, and am trying to make different things happen when it collides with a wall or a goal.
Right now my code is as follows:
public class PlayerMoveScript : MonoBehaviour {
void Update () {
//If exactly one finger is touching
if (Input.touchCount == 1) {
//Set the touch
Touch touch = Input.GetTouch (0);
//Set position of the player to relative screen location
transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (touch.position.x,
touch.position.y, 10));
}
}
void OnTriggerEnter2D(Collider2D coll) {
Debug.Log ("HERE");
if (coll.gameObject.tag == "Wall"){
Debug.Log("Loser!!");
} else if (coll.gameObject.tag == "Goal") {
Debug.Log ("Winner!!");
}
}
}
(I have the Z-axis of movement set to 10 because my camera is at -10, this sets it to 0, which is the same level that all of my walls are.)
I currently have the trigger set on all of my walls an not on my player, along with the walls having RigidBody2d and being set to kinematic. I have tried having triggers on both the walls and the player, along with just the player. I have googled around ad nothing seems to b helping or working.
Player Settings:
Wall Settings:
Thank you.
Your code works fine for me, but you shouldn't set transform.position directly on a rigidbody, you should use $$anonymous$$ovePosition ins$$anonymous$$d. Have you got your collision layer matrix setup correctly?
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D(Collider2D other) 2 Answers
Can go through a trigger,but movement becomes slow,and raycast can't detect 0 Answers
OnTriggerEnter2D not working 2 Answers
How to start a boss fight??? 1 Answer
Weird 2d collision behaviour 1 Answer