- Home /
Detecting a player "on the right path"?
Hello every one,
This is my first question and I come from Belgium.
I'm creating a game with a friend, he does the sound. The idea is to follow an "invisible path". If you leave the path, weird sounds are happening and environment is changing. So I want the controller to follow a path. And when he leaves it, he enters the "zone". On right and left of the path, I place 2 invisible long meshes that delimitate the path. They are not straight, made with blender. IsTrigger is On.
So here is my problem : I try to get a "true" if I'm inside a mesh and "false" if I'm out. The only way to get a volume detection between a mesh collider and a character controller is to trigger the "convex" mode On. With Primitive Shapes Colliders, if I'm inside a giant box, the method OnTriggerExit only sends a message if I leave the entire volume. With a Mesh Collider, as soon as you've entered the mesh, the OnTriggerExit message is sent, just after the OnTriggerEnter method. The OnTriggerStay method doesn't change anything. Only the surfaces work as a trigger. So I have to 'check' the convex mode On, and it works for shapes that have less than 255 triangles. If higher the number, he redisigned the collider shape to maximise the number of triangles at 255. If I have a path like an 'S', The collider doesn't fit anymore on the mesh. It seems I can't decide wich triangles are selected.
So I think I reached a limit of Unity but maybe because there is something more I didn't see. A little trigger to activate somewhere. Or maybe because another solution to the "path" problem exists and I couldn't think of it.
I created a scene to describe the problem. I have the .unitypackage or .rar but Ican't post them with the question, it says 'file type invalid'. I don't know if I can post dropbox link but here they are :D :
unity package : https://dl.dropboxusercontent.com/u/50617115/ConvexMeshColliderTest.unitypackage rar : https://dl.dropboxusercontent.com/u/50617115/ConvexMeshColliderTest.rar
In the scene, there are : a terrain, an fpsController, a cube and a "side of the path" with the convex mode On.There is also a script atteched to the 2 meshes to debug.log "in" if the controller is inside of the shape and "out" if he's outside.
Thanks in advance for reading this and for your time,
Unity rules,
LeMoine
Not a direct answer to your question, but did you consider making a 2D ribbon for the path and using Raycast() to deter$$anonymous$$e if the character controller is on/over the path?
Oh, I see! I thought about the 2D ribbon and the OnTriggerStay method. But the thing is that the path goes up and down following the terrain. The altitude always changes. So making a ribbon in Blender would mean a lot of corrections.
But with the Raycast(), I could make a flat 2D ribbon, standing at a few meters above the player, and raycasting upwards, to see if the player is under it. In that way, event if the altitude changes, I would still be under the ribbon.
That's a great idea, I'll test that as soon as I can and let you know if this is a viable option.
Thanks robertbu,
Le$$anonymous$$oine
Answer by LeMoine · May 28, 2013 at 12:33 AM
Thanks guys,
Actually, the easiest way was robertbu's. I have to admit that it was so easy, I didn't even try Dennis' solution.
I won't post a complete scene because the solution is really easy to reproduce.
I tried first with a 2D plane above the player but I had a little problem, the collision with the raycast only happens if the plane is oriented with the visible face facing down. But then I couldn't see the plane anymore from above! So I reproduced a long snake cube in blender that I placed above the player. I used a mesh collider, put IsTrigger On and created a little script to raycast up. It works perfectly! I know it's beginner level, but here is the script I placed on the Character :
using UnityEngine;
using System.Collections;
public class sceneMeshedPath : MonoBehaviour {
bool isOnPath = false;
void Update () {
RaycastHit hit;
if(Physics.Raycast (transform.position, transform.up, out hit, 50)) {
if(hit.collider.gameObject.tag=="Path"){
isOnPath = true;
}
}else{
isOnPath = false;
}
Debug.Log(isOnPath);
}
}
Thanks again robertbu! Dennis, I promise one day, I'll test your solution :D
LeMoine
Answer by DennisVH · May 26, 2013 at 01:19 PM
I think, based on your information of what you're trying to achieve, it's much easier to just store the points of the path in an array and calculate the closest distance to the path. If a distance surpasses a certain threshold you can flag the player as too distant. And you can have your sound react to the distance too.
static public Vector3 ClosestPointOnLine(Vector3 a, Vector3 b, Vector3 p)
{
return Vector3.Project(p-a, b-a)+a;
}
You only have to just loop through your line segments and collect the smallest distance (pythagoras). Then find the closest distance to that line by using function above. Where a is start of/first point on your line, b is end of/second point on your line and p is your player position. Do a Vector3.Distance on the result. Works in 3d so elevations are no problem.
Hmm, thanks for your answer DennisVH.
I think I understand what you mean, it seems more complicated (mathematically) to implement than the raycast solution. But once the scripts are written, it might be easier to create new paths (maybe for other levels).
I'll try your solution along with the solution of Robertbu during the week and i'll post both solutions in a dedicated scene and mark the post as "answered".
Have a nice sunday,
Le$$anonymous$$oine
Your answer
![](https://koobas.hobune.stream/wayback/20220613102925im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simple Waypoints Glitch 0 Answers
Collider Mesh Is Not Tight Enough 0 Answers
Player Going Backwards on Waypoint Path Issue 0 Answers