- Home /
OnTriggerExit not called for Tree painted on Terrain
public void OnTriggerExit(Collider other)
{
if (other.gameObject.layer == _layer)
{
Debug.Log("OnTriggerExit: " + other.gameObject.name);
_blocked = false;
}
}
public void OnTriggerEnter(Collider c)
{
if (((1 << c.gameObject.layer) & Blocking) != 0)
{
Debug.Log("OnTriggerEnter: " + c.gameObject.name);
_blocked = true;
}
}
When the tank hits an object it Instantiates this test object and rotates it left and right per frame until the OnTriggerExit is called. I have 2 videos linked where it works and then doesn't. It works when i have a Tree Prefab placed on the Terrain from the Assets, but it does not work when the same Tree Prefab is painted onto the Terrain using the Terrain tools in the inspector.
Does work
Does not work
I have a prefab with a collider on it to test if an area is free. The code for the Methods are as simple as it gets. Even when the Debug lines are outside the if statement they are still not printed, so the methods are never called.
So what it seems is that the Collider on the Terrain Tree does not call the OnTriggerExit, but other prefabs manually placed are called. Has anyone see this before?
I am using Unity 5.4.2. I'm not sure what other information is needed, but i'll provide it.
more information could help, like what components are on the GameObjectswith the trigger? does it have a rigidbody? does the tree has a rigidbody and so on. also sharing more of the code could help too.
is the OnTriggerEnter called?
anyway i would make sure that the GameObjects are set right. in order to trigger OnTriggerExit you need at least one of the GameObjects to have a rigidbody. ( as you can see on this page)
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$4Obstaclerator : $$anonymous$$onoBehaviour
{
public bool UNRESOLVED { get; private set; }
public int DegreeJump = 30;
public Layer$$anonymous$$ask Blocking;
private bool _blocked;
private int _layer;
private int[] _steps;
private int _i;
public void Awake()
{
_blocked = true;
_steps = new int[360 / DegreeJump];
var counter = 0;
foreach (var num in nextAngle())
{
if (counter == _steps.Length)
break;
_steps[counter++] = num;
}
}
public void OnTriggerExit(Collider other)
{
if (other.gameObject.layer == _layer)
{
Debug.Log("OnTriggerExit: " + other.gameObject.name);
_blocked = false;
}
}
public void OnTriggerEnter(Collider c)
{
if (((1 << c.gameObject.layer) & Blocking) != 0)
{
Debug.Log("OnTriggerEnter: " + c.gameObject.name);
_blocked = true;
}
}
public void SetColliderLayer(int layer)
{
_layer = layer;
}
public Transform FoundTarget()
{
if (_blocked)
{
transform.parent.RotateAround(transform.parent.position, transform.up, getDegree());
return null;
}
return transform;
}
private int getDegree()
{
if (_i < _steps.Length)
return _steps[_i++];
UNRESOLVED = true;
return 0;
}
private IEnumerable<int> nextAngle()
{
var iRight = 1;
var iLeft = -2;
while (true)
{
yield return iRight * DegreeJump;
yield return iLeft * DegreeJump;
iRight += 2;
iLeft -= 2;
}
}
}
Here is the entire script of the gameObject that i want the OnTriggerExit/Enter to get called. This gameObject that is being rotated only has a box collider with the "Is Trigger" checked and this script attached. Okay i have a render mesh on it as well because i want to see what it is doing for now, hence the big green box in the videos. The Tree Prefab has a RigidBody component.
So it works as expected when i manually place a tree in the scene, but not when i use the same tree with the Terrain tool. The Terrain tool is doing something else that i am unaware of or treats collisions differently to the Prefab - i guess.
from what i can see so far there is not easy way to do what you are trying to do. the trees on the terrain are not the same as other game objects. (as you can see here )
there could be other ways of knowing where the tree is. you can use the terrain data.
i am not 100% sure what you are trying to do with this script, but another thing that could work for you is to add a nav$$anonymous$$esh, then every time you collide with a tree you could get the closest point on the nav$$anonymous$$esh.
i notice that the collider returned from the collision was that of the terrain, not any individual tree. I cannot find it now, but someone recommended a mass placement tool, which seems to be my solution.
What i am trying to do is a simple navigation around a small obstacle that may or may not be there. I don't think the nav$$anonymous$$esh works for dynamic objects? There will be other tanks moving around, or not (knocked out). For larger objects i have a path with target points that will be used to negotiate it.
i am very new to Unity, so my approach will be more than likely wrong! Thanks for the help.
Sounds like you should give navmesh a chance. check out the Nav$$anonymous$$esh Obstacle it could help you. Also there are a lot of pathfinding plugins in the asset store that could help you. Some of them are free.
nav$$anonymous$$esh looks like it has a place when building a game, but it doesn't suit me on this occasion. Anyway, onwards i go!