- Home /
OnTriggerEnter not working in running game
I am working on an infinite running game, but it's not working. You are just passing right through the obstacles. I have two scripts, one called PlayerHandler, and one called CollisionDetector.
You play as my dad, because the game is for his birthday (but now it's late), and this is how it works. FATHER is a parent to Player, which is a parent to Dad.
There are animations for run, jump, and slide.
FATHER has the PlayerHandler script, Player has a Rigidbody, and Dad has the collider and the CollisionDetector script.
I have a bunch of obstacles that have the tag of Obstacle, with Trigger set to true. Also, I did something but I couldn't figure out what. It was working before I did that, but not anymore.
These are my scripts:
PlayerHandler:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PlayerHandler : MonoBehaviour {
float z;
float y;
float speed;
float yspeed;
private bool ducking;
float x;
public float distance;
public GameObject Dad;
bool dead = false;
public GameObject player;
public Animator anim;
public Text YText;
public Text DeadText;
CollisionDetector cd;
void Start()
{
z = gameObject.transform.position.z;
y = gameObject.transform.position.y;
x = gameObject.transform.position.x;
distance = 0f;
anim = Dad.GetComponent<Animator>();
cd = Dad.GetComponent<CollisionDetector>();
}
// Update is called once per frame
void Update()
{
if(cd.collided == true)
{
dead = true;
}
distance++;
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
speed = (float)-0.2;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
speed = (float)0.2;
}
if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
{
speed = 0;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) && Input.GetKeyDown(KeyCode.RightArrow))
{
speed = 0;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && y < 0.25)
{
yspeed = 0.6f;
anim.Play("Jump");
}
if(y <-0.25 || y > 0.25)
{
anim.Play("Jump");
}
if(player.gameObject.transform.position.y <= 0.8)
{
dead = true;
}
x = x + speed;
y = y + yspeed;
yspeed = yspeed - 0.05f;
if (Input.GetKeyDown(KeyCode.DownArrow) && y <= 0.25)
{
ducking = true;
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
ducking = false;
}
if (ducking)
{
anim.Play("Slide");
yspeed = 0;
}
if (y <= 0.1 && y >= -0.1 && !ducking)
{
yspeed = 0;
anim.Play("Run");
}
if (x < (float)-3)
{
x = x + (float)0.2;
}
if (x > (float)3)
{
x = x - (float)0.2;
}
gameObject.transform.position = new Vector3(x, y, z);
YText.text = "Y: " + player.gameObject.transform.position.y;
if (dead)
{
anim.Play("Jump");
DeadText.text = "dead";
y -= 1;
}
}
} CollisionDetector:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class CollisionDetector : MonoBehaviour {
public bool collided = false;
public Text cd;
// Use this for initialization
void Start () {
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Obstacle")
{
collided = true;
cd.text = "Collided: yes";
}
}
// Update is called once per frame
void Update () {
}
} (The UI elements in the script are just for helping me see what is going on.) Also, if you could help me, for some reason whenever you jump, as you land you just fall through the ground, but only like 1 in 10 times. And you don't fall slow enough when you run over holes.
Any help would be greatly appreciated.
Answer by JeffHardddyyy · Sep 22, 2017 at 12:35 AM
Troubleshooting Tips:
Make sure that your player has a RigidBody
Make sure you have trigger checked (as @n314p said)
They all do have IsTrigger on, and the Player object does have a rigidbody.
Answer by NinjaEntertainment · Sep 22, 2017 at 08:23 PM
void OnTriggerEnter(Collider other) { Debug.Log("it's working now!!") }
but you must set your COLLIDER TO IS TRIGGER
Answer by Eba1337 · Sep 29, 2017 at 09:44 PM
Are you using 2D? If so you need to do OnTriggerEnter2D? and you should try adding the parent/children rigibodies without their own gravity so the triggers would work? also obstacles might need? Not sure tho
Answer by HernandoNJ · Oct 07, 2021 at 02:13 AM
Check out this, 2nd reply
Also set isTrigger
only to one of the gameobjects
Your answer
Follow this Question
Related Questions
Check If Any Objects In Your Game Are Colliding? 1 Answer
OnTriggerEnter2D on an empty game object with a box collider? 1 Answer
OnTriggerEnter Issue? 1 Answer
Destroying object on collision 3 Answers