• 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 04, 2017 at 02:35 PM · animationscripting problemgame

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.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

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 09, 2017 at 05:59 PM 0
Share

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

228 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 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

How to restart a 2d level form start upon player collision with an animated object 0 Answers

Coroutine not starting properly with enemy patrol script 2 Answers

Problem with invisible?? hand and gun 1 Answer

How to Set Animation Position on Slope (animation) help please 1 Answer

Simple Animation Script Question 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges