Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added MeshCollider to RoadMeshCreator.cs V2 #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions PathCreator/Examples/Scripts/RoadMeshCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class RoadMeshCreator : PathSceneTool
[Range(0, .5f)]
public float thickness = .15f;
public bool flattenSurface;
public bool meshCollider;

[Header("Material settings")]
public Material roadMaterial;
Expand All @@ -20,16 +21,19 @@ public class RoadMeshCreator : PathSceneTool
MeshFilter meshFilter;
MeshRenderer meshRenderer;

Transform meshHolder;

protected override void PathUpdated()
{
if (pathCreator != null)
{
AssignMeshComponents();
AssignMaterials();
meshFilter.mesh = CreateRoadMesh();;
meshFilter.mesh = CreateRoadMesh();
UpdateMeshCollider(meshCollider);
}
}


Mesh CreateRoadMesh()
{
Expand Down Expand Up @@ -133,7 +137,7 @@ void AssignMeshComponents()
{
// Find/creator mesh holder object in children
string meshHolderName = "Mesh Holder";
Transform meshHolder = transform.Find(meshHolderName);
meshHolder = transform.Find(meshHolderName);
if (meshHolder == null) {
meshHolder = new GameObject(meshHolderName).transform;
meshHolder.transform.parent = transform;
Expand Down Expand Up @@ -166,5 +170,30 @@ void AssignMaterials()
}
}

//update meshcolider if enabled
void UpdateMeshCollider(bool meshColliderEnable)
{
if (meshHolder != null)
{
MeshCollider meshCol = meshHolder.GetComponent<MeshCollider>();
if (meshCol != null)
{
if (meshColliderEnable)
{
meshCol.sharedMesh = meshHolder.GetComponent<MeshFilter>().sharedMesh;
}
else
{
DestroyImmediate(meshCol);
}
}
else if (meshColliderEnable)
{
meshHolder.gameObject.AddComponent<MeshCollider>();
}
}
}


}
}