Hi. I have been using Unity for 2 months, trying to create a simple maze game using Unity 2D. I am using Unity version 2020.1.1. I am simply trying to figure out how to detect when Player reaches TempEP (the name I am using for the maze end point). I will show my scripts and my Player and TempEP inspectors at the end of this entry. I am currently trying to use trigger to determine when Player reaches TempEP and thus using the function OnTriggerEnter2D, but the function never seems to get called. My question is, why is this function never getting called? What am I missing or doing wrong? Is there a tutorial that will help me with this specific situation?
I have tried reading the Unity documentation, Unity forums, and searches on Google, but either everything has to do with two moving objects or what I have tried ends with the same result. The function does not seem to get called. To detect whether the function gets called, I have Debug.Log("triggerObject = " + triggerObject.name); as the first thing inside the function and I never see any response at all on the console.
In the picture “Maze_Player_Collides_with_TempEP”, the arrow is Player and the green square is TempEP. This shows what I am trying to detect.
As you can see from the two scripts below, I tried to use OnTriggerEnter2D in the PlayerController.cs script and also in the EndPointManager.cs script but the debug line does not show up in the console either way I try.
I also included a screenshot of the Player inspector and the TempEP inspector.
Please
This code below is from PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Prime31;
public class PlayerController : MonoBehaviour
{
//player control parameters
public CharacterController2D.CharacterCollisionState2D flags;
public float walkSpeed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float doubleJumpSpeed = 4.0f;
//player ability toggles
public bool canDoubleJump = true;
public bool isGrounded;
public bool isJumping;
public bool isFacingRight;
public bool doubleJumped;
//private variable
private Vector3 _moveDirection = Vector3.zero;
private CharacterController2D _characterController;
// Start is called before the first frame update
void Start()
{
_characterController = GetComponent<CharacterController2D>();
}
// Update is called once per frame
void Update()
{
_moveDirection.x = Input.GetAxis("Horizontal");
_moveDirection.x *= walkSpeed;
if (isGrounded) //player is grounded
{
_moveDirection.y = 0;
isJumping = false;
doubleJumped = false;
if (_moveDirection.x < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
isFacingRight = false;
}
else if (_moveDirection.x > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
isFacingRight = true;
}
if (Input.GetButtonDown("Jump"))
//if (Input.GetAxis("Vertical") != 0)
{
_moveDirection.y = jumpSpeed;
// _moveDirection.y = Input.GetAxis("Vertical");
//_moveDirection.y *= walkSpeed;
isJumping = true;
//Debug.Log("Button is getting registered");
}
}
else //player is in the air
{
if (Input.GetButtonUp("Jump"))
{
if (_moveDirection.y > 0)
{
_moveDirection.y = _moveDirection.y * 0.5f;
}
}
if (Input.GetButtonDown("Jump"))
{
if (canDoubleJump)
{
if (!doubleJumped)
{
_moveDirection.y = doubleJumpSpeed;
doubleJumped = true;
}
}
}
}
_moveDirection.y -= gravity * Time.deltaTime;
_characterController.move(_moveDirection * Time.deltaTime);
flags = _characterController.collisionState;
isGrounded = flags.below;
if (flags.above)
{
_moveDirection.y -= gravity * Time.deltaTime;
}
}
/* public void OnTriggerEnter2D(Collider2D triggerObject)
{
Debug.Log("triggerObject = " + triggerObject.name);
if (triggerObject.gameObject.CompareTag("EndPoint"))
{
Debug.Log("Reached the target");
}
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("Collision occurred");
if (collision.gameObject.CompareTag("EndPoint"))
{
Debug.Log("Reached the target");
}
}*/
}
This code is from EndPointManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EndPointManager : MonoBehaviour
{
float endPointPosX;
float endPointPosY;
// Start is called before the first frame update
void Start()
{
}
public void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("Collision occurred");
}
/* private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Collision occurred");
if (collision.gameObject.CompareTag("EndPoint"))
{
Debug.Log("Reached the target");
}
}*/
}