- Home /
Cannot update meshcollider mesh?
Hey, so, I'm working on some mesh deformation thing, but running into some problem with the collider. Basically, it has a mesh collider and I cannot get it to update itself.
//This should be an icosphere
public Mesh mesh;
public MeshCollider mcollider;
List<TerrainOperation> currentOperations = new List<TerrainOperation>();
// Use this for initialization
void Start () {
mesh = GetComponent<MeshFilter>().mesh;
mcollider = GetComponent<MeshCollider>();
}
void Update(){
//do all terrain operations
if(currentOperations.Count > 0){
for(int i = 0; i < currentOperations.Count; i++){
currentOperations[i].Operate();
if(currentOperations[i].isFinished) currentOperations.Remove(currentOperations[i]);
}
//this does nothing?
mcollider.sharedMesh = mesh;
//update collider -- this is too slow
//DestroyImmediate(mcollider);
//mcollider = gameObject.AddComponent<MeshCollider>();
}
}
My mesh updates just fine, so I thought, well, let's assign the updated mesh as the mesh for the meshcollider, no need to calculate everything again... unfortunately, nothing happens. I've tried just setting the vertices, but that also has no effect except that my mesh becomes permanently altered (still changed when I exit play mode, which I do not want).
The commented out code below that destroys the meshcollider and then creates a completely new one works fine, but is way too slow.
So, thoughts?
Answer by hbalint1 · Apr 12, 2015 at 10:32 PM
Maybe this can help:
http://answers.unity3d.com/questions/446910/changing-mesh-collider-at-run-time.html
Hey, thank you for your reply. Unfortunately, when I try to assign null to the collider mesh, and then assign the mesh again, I have no collision at all anymore, so it doesn't quite work.
(If I don't do that, I just keep my old mesh for the collider.)
Okay, after trying things out some more, that actually works...sometimes. And when it does it's just as slow as destroying and then adding a new meshcollider :(.
Guess I'll have to think some more on how to speed this up... (Yeah, sure 'don't update your meshcollider in update' is all well and good, but I'm not sure if that's really an option).
I make a game where you can tracnsform into other objects and i used the same method. It works for me and it's really quick. I show you:
public void Transform(string name)
{
print("There is a(n) " + name + " in front of the Player!");
// Find the GameObject only once and store it for performance reasons
GameObject target = GameObject.Find(name);
// Change object mesh
gameObject.GetComponent<$$anonymous$$eshFilter>().mesh = target.GetComponent<$$anonymous$$eshFilter>().mesh;
// Change object collider
if (target.collider.GetType() == typeof($$anonymous$$eshCollider))
{
my$$anonymous$$eshCollider.enabled = true;
my$$anonymous$$eshCollider.shared$$anonymous$$esh = target.GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh;
my$$anonymous$$eshCollider.convex = true;
}
}
that way of doing it is fine for an instant transformation, but I want $$anonymous$$e to be gradual, which means I'd have to update the mesh collider basically every frame...which is too slow :<
I think what I need is a heightmap-based collider that is wrapped around a sphere somehow. Should be doable but idk how right now... ugh.
accepting this answer because it did answer my question in the end even though it didnt work out like I wanted :|
Your answer
Follow this Question
Related Questions
Problem with collision detection in an animated object 1 Answer
2D Mesh Deformation? 0 Answers
moving hole 3 Answers
Why is raycast not working with mesh collider 0 Answers
How to remove internal triangle/faces when combining mesh 0 Answers