Jump to content

Detect if an unit is on a road?


Znimu
Go to solution Solved by Manny,

Recommended Posts

Hello,

I have tried several possibilities, but nothing works.

I have seen that the roads tags and layers are upated on start to "ground" and I can't use a raycast if I understand well.

Is there a known solution?

Link to comment
Share on other sites

I have set the tag "Roads" to the Road Meshes empty.

Trying the following code, but sometimes it finds that the character is out of the road:

Ray myRay2 = new Ray(transform.position, -transform.up);
RaycastHit hitInfo2;

if (Physics.Raycast(myRay2, out hitInfo2, 100)) {
    if (hitInfo2.collider.transform.parent.tag == "Roads") {
        Debug.Log("On the road again!");
    }
    else {
        Debug.Log("Out!");
    }
}

 

Link to comment
Share on other sites

Can you show me where the tag gets set to ground when you specify "roads"?
Is this when you click Play or during a build?

 

One thing that you might try is to lift the starting point for the raycast up a bit.
 

Ray myRay2 = new Ray(transform.position + Vector3.up, -transform.up);

That begins the raycast 1 unit above the position.

Link to comment
Share on other sites

Another question, @Znimu

Why are you checking the 

hitInfo2.collider.transform.parent.tag

instead of 

 

hitInfo2.collider.transform.tag

?

Are the mesh gameobjects not tagged?

Link to comment
Share on other sites

  • 2 weeks later...

The way I detect a road for my city generator is that I do a box cast down and I filter out all the hits that don't contain the GeNaRoad component. I tried to keep from adding any different layers or tags in my project other than what is already in the Procedural Worlds Assets. You'll wanna do a foreach loop on the hits. So run the Physics.Boxcast(new Vector3(ThePositionOfYourTransform.x,ThePositionOfYourTransform.y+SomeAddedHeightSoYouDefinitelyAreAboveTheTerrain,ThePositionOfYourTransform.z ), Vector3.down, out hits, lengthToCast). Then foreach(raycastHit hit in hits) you'd wanna check if the hit collider can find the GeNaRoad component in the hit gameobject of the collider. So basically if (!hit.collider.getcomponent<GeNaRoad>()) then continue and else return the type you want to find. Probably the collider or GameObject. I found it difficult to reliably detect the road just using a raycast. I don't know why. But I did read that it's harder to detect nonconvex mesh colliders. You'd best run this in its own method and not in update because it requires looping.

Link to comment
Share on other sites

  • 5 months later...
  • Solution
On 6/30/2022 at 10:16 AM, RickysSoftware said:

The way I detect a road for my city generator is that I do a box cast down and I filter out all the hits that don't contain the GeNaRoad component. I tried to keep from adding any different layers or tags in my project other than what is already in the Procedural Worlds Assets. You'll wanna do a foreach loop on the hits. So run the Physics.Boxcast(new Vector3(ThePositionOfYourTransform.x,ThePositionOfYourTransform.y+SomeAddedHeightSoYouDefinitelyAreAboveTheTerrain,ThePositionOfYourTransform.z ), Vector3.down, out hits, lengthToCast). Then foreach(raycastHit hit in hits) you'd wanna check if the hit collider can find the GeNaRoad component in the hit gameobject of the collider. So basically if (!hit.collider.getcomponent<GeNaRoad>()) then continue and else return the type you want to find. Probably the collider or GameObject. I found it difficult to reliably detect the road just using a raycast. I don't know why. But I did read that it's harder to detect nonconvex mesh colliders. You'd best run this in its own method and not in update because it requires looping.

Hey @RickysSoftware

Could you please confirm if you are still experiencing the same issue with the latest version of GeNa Pro?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Tell a friend

    Love Canopy - Procedural Worlds? Tell a friend!
  • Need help?

    We work with some of the biggest brands in global gaming, automotive, technology, and government to create environments, games, simulations, and product launches for desktop, mobile, and VR.

    Our unique expertise and technology enable us to deliver solutions that look and run better at a fraction of the time and cost of a typical project.

    Check out some of our non-NDA work in the Gallery, and then Contact Us to accelerate your next project!

×
×
  • Create New...