- 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.
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 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 from @anybody.
Answer by TeohRIK · Sep 07, 2017 at 03:20 AM
I not sure am I correct or not
The reason why your it wont trigger is because the way you update the player position is wrong
gameObject.transform.position = new Vector3(x, y, z); // wrong
You might need to try with others method like using Transfrom.Translate or add velocity to the rigidbody
nope, didn't work
just speeds off of the screen when i press left or right, and goes down super fast when i want to jump.
Your answer
Follow this Question
Related Questions
Is there a new retargetting system for the animation in 5.5? 1 Answer
How do I stop moving when attacking? 1 Answer
Can I make animations snap to a frame? 1 Answer
What does Mecanim DO? 3 Answers