- Home /
Wheelcollider has zero friction on run-time created mesh
I'm creating a mesh at run-time by adding new vertices etc. to it and I want a vehicle to drive on it. Problem is that the vehicle simply slides of with no friction at all! And its only with the wheelcolliders, other colliders work fine (have friction). For instance if I flip the vehicle on its roof it has friction as intended. And its also only for the run-time created mesh. The wheelcolliders work fine on terrain / other collider / other static mesh.
This is the code used to create the mesh. leftSpawner and rightSpawner are simply two objects that move around to specify the coordinates for new mesh vertices. As for the wheelcolliders no setting I change makes any difference (default doesn't work either) - but it's only for the created mesh, else they work fine.
EDIT: I want to point out that the code below isn't the main focus here. Has anyone got wheelcolliders to work on a runtime created mesh at all?? The code is only posted in case anyone spots something weird with it. But if anyone has wheelcolliders working in any way on a runtime created mesh please let me know!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnGroundMesh : MonoBehaviour
{
public float spawnInterval;
private const int maxVertices = 512;
private List<Vector3> newVertices;
private List<int> newTriangles;
private List<Vector2> newUV;
private List<Vector4> newTangents;
private Mesh newMesh;
private GameObject leftSpawner;
private GameObject rightSpawner;
// Use this for initialization
void Start ()
{
newMesh = GetComponent<MeshFilter>().mesh;
leftSpawner = GameObject.FindWithTag("LeftPos");
rightSpawner = GameObject.FindWithTag("RightPos");
newVertices = new List<Vector3>(maxVertices);
newVertices.Add(leftSpawner.transform.position);
newVertices.Add(rightSpawner.transform.position);
newTriangles = new List<int>((maxVertices - 2) * 3);
newUV = new List<Vector2>(maxVertices);
newUV.Add(new Vector2(leftSpawner.transform.position.x
, leftSpawner.transform.position.z));
newUV.Add(new Vector2(rightSpawner.transform.position.x
, rightSpawner.transform.position.z));
newTangents = new List<Vector4>(maxVertices);
Vector3 newTangent = newVertices[newVertices.Count-1]
- newVertices[newVertices.Count-2];
newTangents.Add(new Vector4(newTangent.x, newTangent.y, newTangent.z, 0));
newTangents.Add(new Vector4(newTangent.x, newTangent.y, newTangent.z, 0));
SpawnNewPoints();
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate()
{
if(Vector3.Distance(rightSpawner.transform.position
, newVertices[newVertices.Count-3]) > spawnInterval)
{
SpawnNewPoints();
}
UpdateAtatchedPoints();
newMesh.Clear();
newMesh.vertices = newVertices.ToArray();
newMesh.uv = newUV.ToArray();
newMesh.triangles = newTriangles.ToArray();
newMesh.RecalculateNormals();
newMesh.tangents = newTangents.ToArray();
GetComponent<MeshFilter>().mesh = newMesh;
GetComponent<MeshCollider>().sharedMesh = null;
GetComponent<MeshCollider>().sharedMesh = newMesh;
}
private void UpdateAtatchedPoints()
{
newVertices[newVertices.Count-2] = leftSpawner.transform.position;
newVertices[newVertices.Count-1] = rightSpawner.transform.position;
}
private void SpawnNewPoints()
{
//make space if full
if(newVertices.Count >= maxVertices)
{
RemoveOldPoints();
//add vertices
newVertices.Add(leftSpawner.transform.position);
newVertices.Add(rightSpawner.transform.position);
}
else
{
//add vertices
newVertices.Add(leftSpawner.transform.position);
newVertices.Add(rightSpawner.transform.position);
//add triangle list
newTriangles.Add(newVertices.Count - 4);
newTriangles.Add(newVertices.Count - 2);
newTriangles.Add(newVertices.Count - 1);
newTriangles.Add(newVertices.Count - 4);
newTriangles.Add(newVertices.Count - 1);
newTriangles.Add(newVertices.Count - 3);
}
//add uv coords
newUV.Add(new Vector2(leftSpawner.transform.position.x
, leftSpawner.transform.position.z));
newUV.Add(new Vector2(rightSpawner.transform.position.x
, rightSpawner.transform.position.z));
//add tangents
Vector3 newTangent = newVertices[newVertices.Count-1]
- newVertices[newVertices.Count-2];
newTangents.Add(new Vector4(newTangent.x, newTangent.y, newTangent.z, 0));
newTangents.Add(new Vector4(newTangent.x, newTangent.y, newTangent.z, 0));
}
private void RemoveOldPoints()
{
newVertices.RemoveRange(0, 2);
newUV.RemoveRange(0, 2);
newTangents.RemoveRange(0, 2);
}
}
If you format the code with the 101010 button you'll have better luck getting someone to read through this one :)
$$anonymous$$y eyes... they burn... 1010101 pleaseeeeee....
Huh? It already is!
Just to make sure I turned 1010101 off/on again and asked a friend to view it. Looks fine to him. Is it fixed now?