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 /
  • Help Room /
avatar image
0
Question by EmmanuelMF · Feb 28, 2018 at 08:33 AM · triggerfunctiontriggersfunctions

OnTriggerEnter not working

This code on unity doesn't recognice the triggers in the functions using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections.Generic; using UnityEngine;

 public class SnakeMovement : MonoBehaviour {
     public List<Transform> BodyParts = new List<Transform>();
     public float mindistance = 0.25f, limx, limy, limz;
     public float speed =1;
     public float rotationspeed = 50;
     public int beginsize;
     public float TimeFromLastRetry;
     public GameObject boddypfrefab;
 
     public GameObject DeadScreen;
 
     private float dis, x, y, z;
     private Transform curBodyPart;
     private Transform prevBodypart;
 
     private Vector3 pos;
     
     
     public Text winText;
     public Text timerText;
 
     private Rigidbody rb;
     private int count;
     public Text countText;
     public GameObject particulas;
 
     private float StartTime;
 
     // Use this for initialization
     void Start () {
         count = 0;
         SetCountText();
         rb = GetComponent<Rigidbody>();
         StartTime = Time.time;
         StartLevel();
     }
 
     public void StartLevel()
     {
         //TimeFromLastRetry = Time.time;
         DeadScreen.SetActive(true);
         for (int i = 0; i < beginsize - 1; i++)
         {
             AddBodyPart();
         }
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         Move();
         if (Input.GetKey(KeyCode.Q))
         {
             AddBodyPart();
         }
         float t = Time.time - StartTime;
         string minutes = ((int)t / 60).ToString();
         string seconds = (t % 60).ToString("f2");
         timerText.text = minutes + ":" + seconds;   
         //BodyParts[0].position = new Vector3(2, 0.5f, 0);
     }
 
     public void Move()
     {
         float curspeed = speed;
 
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         BodyParts[0].forward += movement;
         if (Input.GetAxis("Vertical") != 0 )
         {
             curspeed *= 2;
             
         }
         BodyParts[0].Translate(BodyParts[0].forward * curspeed * Time.smoothDeltaTime, Space.World);
         //BodyParts[0].Translate(movement*curspeed);
         //BodyParts[0].Translate(BodyParts[0].forward*curspeed*Time.smoothDeltaTime, Space.World);
 
         if (Input.GetAxis("Horizontal") != 0)
             BodyParts[0].Rotate(Vector3.up * rotationspeed * Time.deltaTime * Input.GetAxis("Horizontal"));
 
         for (int i=1; i<BodyParts.Count; i++)
         {
             curBodyPart = BodyParts[i];
             prevBodypart = BodyParts[i - 1];
 
             dis = Vector3.Distance(prevBodypart.position, curBodyPart.position);
 
             Vector3 newpos = prevBodypart.position;
 
             newpos.y = BodyParts[0].position.y;
 
             float T = Time.deltaTime * dis / mindistance * curspeed;
 
             if(T>0.5f)
             {
                 T = 0.5f;
             }
             curBodyPart.position = Vector3.Slerp(curBodyPart.position, newpos, T);
             curBodyPart.rotation = Quaternion.Slerp(curBodyPart.rotation, prevBodypart.rotation, T);
         }
     }
 
     public void AddBodyPart()
     {
         Transform newpart = (Instantiate(boddypfrefab, BodyParts[BodyParts.Count-1].position, BodyParts[BodyParts.Count - 1].rotation) as GameObject).transform;
         newpart.SetParent(transform);
 
         BodyParts.Add(newpart);
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.CompareTag("Objetivo"))
         {
             Instantiate(particulas, other.gameObject.transform.position, Quaternion.identity);//creacion efecto de choqu
             other.gameObject.transform.position = new Vector3(-999, -999, -999);
             x = Random.Range(-limx, limx);
             y = limy;
             z = Random.Range(-limz, limz);
             pos = new Vector3(x, y, z);
             other.gameObject.transform.position = pos;//traslado a nueva locación
             count++;
             SetCountText();
             print("trigger objetivo");
             AddBodyPart();
         }
         if(other.gameObject.CompareTag("Pared"))
         {
             print("trigger con pared");
             Instantiate(particulas, other.gameObject.transform.position, Quaternion.identity);//creacion efecto de choque
             Scene scene = SceneManager.GetActiveScene();
             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
         }
     }
     void SetCountText()
     {
         countText.text = "Count: " + count.ToString();
         /*if (count >= 12)
         {
             Scene scene = SceneManager.GetActiveScene();
             winText.text = "You Win!!! :)";
             if (scene.name == "minigame")
             {
                 SceneManager.LoadScene(1);
             }
             else if (scene.name == "minigame2")
             {
                 SceneManager.LoadScene(2);
             }
             else if (scene.name == "minigame3")
             {
                 SceneManager.LoadScene(3);
             }
             else if (scene.name == "minigame4")
             {
                 Application.Quit();
             }
         }*/
     }
 }


,I'm making a snake game but the OnTriggerEnter function does not recibe anything or it is not working this is the code

 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SnakeMovement : MonoBehaviour {
     public List<Transform> BodyParts = new List<Transform>();
     public float mindistance = 0.25f, limx, limy, limz;
     public float speed =1;
     public float rotationspeed = 50;
     public int beginsize;
     public float TimeFromLastRetry;
     public GameObject boddypfrefab;
 
     public GameObject DeadScreen;
 
     private float dis, x, y, z;
     private Transform curBodyPart;
     private Transform prevBodypart;
 
     private Vector3 pos;
     
     
     public Text winText;
     public Text timerText;
 
     private Rigidbody rb;
     private int count;
     public Text countText;
     public GameObject particulas;
 
     private float StartTime;
 
     // Use this for initialization
     void Start () {
         count = 0;
         SetCountText();
         rb = GetComponent<Rigidbody>();
         StartTime = Time.time;
         StartLevel();
     }
 
     public void StartLevel()
     {
         //TimeFromLastRetry = Time.time;
         DeadScreen.SetActive(true);
         for (int i = 0; i < beginsize - 1; i++)
         {
             AddBodyPart();
         }
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         Move();
         if (Input.GetKey(KeyCode.Q))
         {
             AddBodyPart();
         }
         float t = Time.time - StartTime;
         string minutes = ((int)t / 60).ToString();
         string seconds = (t % 60).ToString("f2");
         timerText.text = minutes + ":" + seconds;   
         //BodyParts[0].position = new Vector3(2, 0.5f, 0);
     }
 
     public void Move()
     {
         float curspeed = speed;
 
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         BodyParts[0].forward += movement;
         if (Input.GetAxis("Vertical") != 0 )
         {
             curspeed *= 2;
             
         }
         BodyParts[0].Translate(BodyParts[0].forward * curspeed * Time.smoothDeltaTime, Space.World);
         //BodyParts[0].Translate(movement*curspeed);
         //BodyParts[0].Translate(BodyParts[0].forward*curspeed*Time.smoothDeltaTime, Space.World);
 
         if (Input.GetAxis("Horizontal") != 0)
             BodyParts[0].Rotate(Vector3.up * rotationspeed * Time.deltaTime * Input.GetAxis("Horizontal"));
 
         for (int i=1; i<BodyParts.Count; i++)
         {
             curBodyPart = BodyParts[i];
             prevBodypart = BodyParts[i - 1];
 
             dis = Vector3.Distance(prevBodypart.position, curBodyPart.position);
 
             Vector3 newpos = prevBodypart.position;
 
             newpos.y = BodyParts[0].position.y;
 
             float T = Time.deltaTime * dis / mindistance * curspeed;
 
             if(T>0.5f)
             {
                 T = 0.5f;
             }
             curBodyPart.position = Vector3.Slerp(curBodyPart.position, newpos, T);
             curBodyPart.rotation = Quaternion.Slerp(curBodyPart.rotation, prevBodypart.rotation, T);
         }
     }
 
     public void AddBodyPart()
     {
         Transform newpart = (Instantiate(boddypfrefab, BodyParts[BodyParts.Count-1].position, BodyParts[BodyParts.Count - 1].rotation) as GameObject).transform;
         newpart.SetParent(transform);
 
         BodyParts.Add(newpart);
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.CompareTag("Objetivo"))
         {
             Instantiate(particulas, other.gameObject.transform.position, Quaternion.identity);//creacion efecto de choqu
             other.gameObject.transform.position = new Vector3(-999, -999, -999);
             x = Random.Range(-limx, limx);
             y = limy;
             z = Random.Range(-limz, limz);
             pos = new Vector3(x, y, z);
             other.gameObject.transform.position = pos;//traslado a nueva locación
             count++;
             SetCountText();
             print("trigger objetivo");
             AddBodyPart();
         }
         if(other.gameObject.CompareTag("Pared"))
         {
             print("trigger con pared");
             Instantiate(particulas, other.gameObject.transform.position, Quaternion.identity);//creacion efecto de choque
             Scene scene = SceneManager.GetActiveScene();
             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
         }
     }
     void SetCountText()
     {
         countText.text = "Count: " + count.ToString();
         /*if (count >= 12)
         {
             Scene scene = SceneManager.GetActiveScene();
             winText.text = "You Win!!! :)";
             if (scene.name == "minigame")
             {
                 SceneManager.LoadScene(1);
             }
             else if (scene.name == "minigame2")
             {
                 SceneManager.LoadScene(2);
             }
             else if (scene.name == "minigame3")
             {
                 SceneManager.LoadScene(3);
             }
             else if (scene.name == "minigame4")
             {
                 Application.Quit();
             }
         }*/
     }
 }





[1]: /storage/temp/112118-pared.png

pared.png (108.5 kB)
objetivo.png (111.4 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cuttlas-U · Mar 01, 2018 at 09:11 AM

hey; i dont see any problems in your script or the objects;

can u show me a screen shot from your snake object too ;

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
avatar image
0

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

Both gameobjects must have a collider, one of them must be trigger and also any of them must have attached a Rigidbody. Also keep in mind if it is OnTriggerEnter or OnTriggerEnter2D

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

144 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

Related Questions

check if a function was run 0 Answers

How to disable script on trigger 1 Answer

OnTrigerEnter not working 2 Answers

How to detect collision of two moving characters? 1 Answer

Enemy Trigger Colliders are triggering my player's trigger collider. Why? 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