- Home /
Why when i move the player object through the door the ontriggerenter/exit event are not fire ?
This script is attached to the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class door : MonoBehaviour
{
void OnTriggerEnter(Collider obj)
{
var thedoor = GameObject.FindWithTag("SF_Door");
thedoor.GetComponent< Animation > ().Play("open");
}
void OnTriggerExit(Collider obj)
{
var thedoor = GameObject.FindWithTag("SF_Door");
thedoor.GetComponent< Animation > ().Play("close");
}
}
Also this script is attached to the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointAndClick : MonoBehaviour
{
public float moveSpeed = 50;
private Vector3 originalPosition;
void Start ()
{
originalPosition = transform.position;
}
void Update()
{
if (transform.position.z < 100)
transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
}
}
To the player i added also a Rigidbody component.
The door i added a component box collider. I tried to play with the box collider is trigger to check/uncheck it. I also tried to play with the rigidbody on the player check/uncheck the is kinematic if not checked the player is falling down.
All the cases i tried not working. I added a break point on the OnTriggerEnter in the door script and it's never getting there.
In the screenshot on the left the Hierarchy the Platform and Doors are just empty GameObjects. On Door_Prefab i added a box collider and the is trigger is checked. And the box collider size is set to x = 10 y = 10 z = 10
Then Player is also empty GameObject that i added to it the Rigidbody attached the two scripts and also added to it a box collider.
Tried to play on both the door and player box collider is trigger check/unchecked nothing was working.
Answer by PERFKNIGHT · Sep 18, 2017 at 09:51 PM
You're actually trying to find the first object that is a door in your hierarchy, which probably isn't what you want. You want to get the animation from the specific object you have triggered. Your Collider argument is in the OnTriggerEnter to give you information on what the trigger has collided with.
You can do one of two things: change the FindObject command to a CompareTag method (I'll show how to code that later), or use a combination of input and raycasting to open doors manually rather than automatically.
First solution:
void OnTriggerEnter(Collider obj)
{
if(obj.CompareTag("Door"))
{
Animation open = obj.gameObject.GetComponent<Animation>("Open");
open.Play();
}
}
You can do this for both animations.
The second method involves raycasting and input. Watch a few tutorials on that to get the best idea, but basically, what you want to do is make sure you're casting a ray at the door when you press a button. If the door is within range and the button is pressed, the door opens/closes.
Hope this helps!
I found the problem and it was my mistake. First to trigger the events i'm using now on the player capsule collider easy to set it to cover the player. And a rigidbody also on the player.
On the door like before a box collider.
On the Rigidbody only Use Gravity is checked. On both box collider and capsule collider Is Trigger is unchecked. The two scripts are attached like before to the player.
Now the events are working. And since i have two doors duplicated and both using same tag "SF_Door" took me some time to figure out it's working fine it's just opened the second door and i was looking on the first one.
Anyway now it's working.
Answer by SoulPixel · Sep 18, 2017 at 09:06 PM
Try to use "Debug.Log" to check, if the collision trigger works. Then you will first of all know, if the problem is the collision system or your code, attached to it.