- Home /
Trigger on MeshCollider Only Seems To Work at Edges of Mesh
I've got an area of my game world where I want fog and I'm using a trigger to change the render settings. I've made a mesh with the correct shape in Blender and attached this script to it
using UnityEngine;
using System.Collections;
public class SwampFogScript : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
RenderSettings.fogColor = Color.green;
RenderSettings.fogDensity = 0.1f;
RenderSettings.fog = true;
}
void OnTriggerExit(Collider other)
{
RenderSettings.fog = false;
}
}
The fog only shows for a split second when you walk into the trigger. It's almost as if only the faces of the mesh are counted as the trigger. How can I overcome this?
Try setting the $$anonymous$$eshCollider to Convex, and use multiple convex colliders ins$$anonymous$$d of one concave one.
For some reason, if I turn on convex, it's just always returning true whether the player character is in the trigger or not.
Answer by carnivoris · Jan 25, 2015 at 01:20 AM
I fixed it. I ended up using a boolean to check whether I had entered the mesh object in my OnTriggerExit function
void OnTriggerExit(Collider other)
{
if (inSwamp == false)
{
inSwamp = true;
RenderSettings.fogStartDistance = 5.0f;
RenderSettings.fogColor = fogColor;
RenderSettings.fogDensity = 0.01f;
RenderSettings.fog = true;
}
else
{
inSwamp = false;
RenderSettings.fog = false;
}
}
Since every time I enter the trigger, I also seem to exit it instantly, all the code I was using for OnTriggerEnter actually works in OnTriggerExit. The boolean keeps track of how many times you've exited the trigger mesh.
Your answer
Follow this Question
Related Questions
OnTriggerEnter/Exit called unexpectedly 2 Answers
On trigger exit not working 2 Answers
Help with OnTriggerEnter issue 3 Answers