Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SoshJam · Sep 21, 2017 at 09:47 PM · collisioncollision detectionontriggerenternot workingcollision-detection

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nt314p · Sep 22, 2017 at 12:11 AM 0
Share

Do you have is trigger checked?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JeffHardddyyy · Sep 22, 2017 at 12:35 AM

Troubleshooting Tips:

  1. Make sure that your player has a RigidBody

  2. Make sure you have trigger checked (as @n314p said)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoshJam · Sep 29, 2017 at 09:41 PM 0
Share

They all do have IsTrigger on, and the Player object does have a rigidbody.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoshJam · Sep 29, 2017 at 09:32 PM 0
Share

They all do have IsTrigger on

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoshJam · Oct 03, 2017 at 12:01 AM 0
Share

I'm not using 2d

avatar image
0

Answer by HernandoNJ · Oct 07, 2021 at 02:13 AM

Check out this, 2nd reply

https://answers.unity.com/questions/1474449/ontriggerenter-not-working-26.html?childToView=1864967#answer-1864967

Also set isTrigger only to one of the gameobjects

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying object on collision 3 Answers

2d Collision not detect player when it falls 2 Answers

Could some one help me with this script... 5 Answers

Check If Any Objects In Your Game Are Colliding? 1 Answer

OnTriggerEnter2D on an empty game object with a box collider? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges