- Home /
OnTriggerEnter not working all the time.
I am developing this faller platformer where the player is to reach the bottom while picking the right platforms to land on. There are also pickups for the player on the way down. When my player lands on the the blue plartforms they are meant to change color and the pickups and meant to deactivate when the player collides with them.
This does not work all the time. Sometimes the player will collide with the plartform or pickup and nothing will happen. Could anyone help me with this.
Here is the Trigger code below.
void OnTriggerEnter2D(Collider2D target)
{
if (target.tag =="Door")
{
cameraScript.moveCamera = false;
DoorUI.SetActive (true);
// Door open and stop the camera.
}
if (target.tag == "Coins")
{
coinCount++;
scoreCount += 200;
AudioSource.PlayClipAtPoint(coinSound, target.transform.position);
target.gameObject.SetActive (false);
}
if (target.tag == "Life")
{
lifeCount++;
scoreCount += 300;
AudioSource.PlayClipAtPoint(lifeSound, target.transform.position);
target.gameObject.SetActive (false);
}
if (target.tag == "Boundary")
{
cameraScript.moveCamera = false;
countPoints = false;
CheckGameStatus();
}
if (target.tag == "Deadly")
{
cameraScript.moveCamera = false;
countPoints = false;
CheckGameStatus();
}
for(int i = 0; i < listener1.Length; i++)
{
if (target.gameObject == listener1[i] )
{
musicBoostCount++;
Debug.Log ("IT HAPPEND");
}
}
for(int i = 0; i < listener2.Length; i++)
{
if (target.gameObject == listener2[i])
{
musicBoostCount++;
Debug.Log ("IT HAPPEND2");
}
}
if (target.tag == "Clouds")
{
grounded = true;
}
}`
Here is a Link to My Dev Blog with Gifs of the game in case anyone needs more info : Link
Answer by Nodgez · Jun 07, 2016 at 09:25 PM
Odds are that your object is moving too fast for the trigger to be calculated, effectively being positioned over the platform one frame and below it the next. You could do 1 of 2 things
Clamp the downward/y movement of the player character so it can only move so quickly
Since the triggers use the physics update you could increase the frequency of the fixed timestep http://docs.unity3d.com/Manual/class-TimeManager.html
Answer by bleachdrinker · Jun 08, 2016 at 08:09 AM
From the gifs it looks like there is a button press or some kind of trigger that allows the player to fall quickly. Like Nodgez said the movement may be to fast to detect the collision. Another suggestion would be to use a raycast to detect the collisions. Shoot a ray out from the feet for a short distance and this will allow more than the single pixel of collision detection. Not sure if this will help or not either but there is also OnCollisionStay. OnCollisionStay should at least help with the power-ups. If You use onCollisonStay I would use it as well as CollisionEnter and have CollisionStay as a backup in case Enter doesn’t trigger. RayCast and RayCast Hit would be something to look into for the fast movement.
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
http://docs.unity3d.com/ScriptReference/RaycastHit.html
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay.html
Your answer
Follow this Question
Related Questions
2d game end level with trigger and colission with trigger who to make a if condition 1 Answer
How do I make animation transitions a one-way street? 1 Answer
My trigger is called twice 1 Answer
OnCollisionEnter2D not being called on entering new tile once already called. 1 Answer
jump on collision enter 1 Answer