Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LeMoine · May 25, 2013 at 11:24 PM · playerpathmesh collidertrianglesconvex

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


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 26, 2013 at 04:09 AM 0
Share

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?

avatar image LeMoine · May 26, 2013 at 11:36 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LeMoine · May 26, 2013 at 01:48 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple Waypoints Glitch 0 Answers

Collider Mesh Is Not Tight Enough 0 Answers

Player Going Backwards on Waypoint Path Issue 0 Answers

Mesh Colliders, best practice for large meshes? 1 Answer

Very Confused About Convex Mesh Collider Result 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges