- Home /
OnTriggerEnter not working as intended
using UnityEngine; using System.Collections;
public class Lvl9EnemyTeaser : MonoBehaviour {
public Transform[] point;
public float speed;
private int currentPos;
// Use this for initialization
void Start () {
gameObject.transform.position = point[0].position;
currentPos = 0;
}
// Update is called once per frame
void Update () {
if (transform.position == point [currentPos].position)
currentPos++;
if (currentPos >= point.Length)
currentPos = 0;
transform.position = Vector3.MoveTowards (transform.position, point [currentPos].position, Time.deltaTime * speed);
}
void OnTriggerEnter (Collider tele){
if (tele.transform.tag == "Teleport") {
print ("triggered!");
transform.position = new Vector3(7.5f, 0, 3.5f);
currentPos++;
}
}
}
Now, this is not a player, this just an object going from point A to point B, but when it hits point B, what should happen is that the object looses its render mesh and another one (reciever) turns it self on and does the same at another location (I really hope that it's at least somewhat understandable).
But, object A doesn't detect the trigger at all. I applied rigidbody to the object (A), and put tag on the object that is supposed to be the trigger (and of course put it as a trigger).
Anyone got any ideas?
P.S. If any part of this is not understandable, feel free to say so, I'll try to make it more simple.
Thank you!
EDIT: Edited the main code (reason posted below)
UPDATE: I rethought what I did there and took out the 2 object - illusion of teleportation idea out, and actually tried to teleport the object, but the problem remains where the object doesn't recognize TriggerEnter event.
the OnTriggerEnter event has changed to:
void OnTriggerEnter (Collider tele){
if (tele.transform.tag == "Enemy") {
print ("triggered!");
transform.position = new Vector3(7.5f, 0, 3.5f);
currentPos++;
}
}
Another P.S. The OnCollisionStay thing from the original post is just something I thought of testing out, but of course, it didn't work.
Does the object that is supposed to trigger the event have a Rigidbody?
Yes, but I haven't configured the rigidbody component on the object. Is that necessary for this to work (I did turn on kinetics, since I didn't want the object to gain gravitational preferences, though)?
Answer by KittyKatKat · Apr 03, 2014 at 11:07 PM
using UnityEngine;
using System.Collections;
public class Lvl9EnemyTeaser : MonoBehaviour {
public Transform[] point;
public float speed;
public AudioClip audioClip;
private int currentPos;
void Start () {
gameObject.transform.position = point[0].position;
currentPos = 0;
}
void Update () {
if (transform.position == point [currentPos].position)
currentPos++;
if (currentPos >= point.Length)
currentPos = 0;
transform.position = Vector3.MoveTowards (transform.position, point [currentPos].position, Time.deltaTime * speed);
if (currentPos == 2 || currentPos == 5) {
transform.position = point[currentPos].position;
TeleportSound ();
}
}
void TeleportSound(){
audio.clip = audioClip;
audio.Play ();
}
}
Well, I'm really not sure why Unity doesn't like my object being detected (Yea, I did use the rigidbody and all needed (I think/hope!), BUT I did find an alternative. It doesn't actually do an OnTriggerEvent, but it solves the issue.
Basicly it tests the 2 positions from which it would normally enter the trigger and just teleports the object to the next location with the sound that I implemented. It still doesn't 'work as intended', but it's a quick fix.
Posted for someone who might have a similar problem and could use this idea.
However, if anyone has any idea what caused the original problem, answers are always welcome. ^^
Cheers.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GameObject tag to if condition 1 Answer
Distribute terrain in zones 3 Answers
AI Attack Not Working! W/Video 1 Answer
Checking Tag of Trigger Prefab 1 Answer