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
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 ;
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
Your answer
Follow this Question
Related Questions
check if a function was run 0 Answers
Pipe Game ... I have to trace water from source to destination 2 Answers
Trigger events with bullets 0 Answers
Trigger the Animation #2 after Animation #1 was triggered 0 Answers
Move a object with a trigger? 0 Answers