- Home /
Prevent lag/delay when enabling mesh collider (convex)
Hey,
I have a selfmade mesh with a mesh collider on it that is used to walk on and now I want to make a building that can be built on this mesh (strategy game like) so I am instantiating a prefab I put in resources. The prefab has a mesh collider too but has convex enabled so it can collide with the other mesh (non-convex mesh collider cannot collide with another non-convex mesh)
The Problem is now, that there is an about 1 second delay when creating the prefab with instantiate.
to reduce and analyse this problem to its ground, I created just a simple mesh with mesh collider convex on it and turn its convex on and off with a button and the problem still exists, here is the simple code:
using UnityEngine;
using System.Collections;
public class Building : MonoBehaviour {
// focus and states and stuff
public int state = 0;
// Use this for initialization
void Start () {
transform.collider.enabled = true;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown (KeyCode.P))
{
transform.collider.enabled = !transform.collider.enabled;
}
}
}
The object that has this script has a mesh collider and rigidbody on it and again there is an unaccaptable long delay / lag when pressing P.
Do you have any idea how to prevent that or what would be the best solution to instantiate prefabs with rigidbody and mesh collider (convex) without having this delay / lag?
Ah and by the way, I cannot enable just convex itself like described here: Unity3d Scripting Manual - MeshCollider.convex MonoDevelop just tells me (like unity itself) that there is no definition "convex" for UnityEngine.Collider
EDIT: Ok sorry I just saw that MonoDevelop doesn't have a problem with transform.collider.convex, just unity3d, here is the Error:
Assets/Standard Assets/Scripts/Building.cs(19,73): error CS1061: Type `UnityEngine.Collider' does not contain a definition for `convex' and no extension method `convex' of type `UnityEngine.Collider' could be found (are you missing a using directive or an assembly reference?)
Answer by ScroodgeM · Jul 20, 2012 at 04:32 AM
cast Collider type to MeshCollider type first. 'convex' is a MeshCollider's property, not Collider's
I don't know what you mean
I can make a mesh collider in the script like
private $$anonymous$$eshCollider meshC;
(does this put a meshcollider on the object even when I don'T put one on it with unity itself?) and later write
meshC.enabled = true;
meshC.convex = true;
but I mean I already put the meshcollider on the object, if I delete it it acts just like the script is useless, so how does this work at all with the mesh collider when the script doesnt work and the component by unity "mesh collider" makes a lag / delay.
I don't get it
you should use something like this in script
private $$anonymous$$eshCollider meshCollider;
and in method changing convex property
if (!meshCollider) { meshCollider = gameObject.GetComponent<$$anonymous$$eshCollider>(); } if (!meshCollider) { meshCollider = gameObject.AddComponent<$$anonymous$$eshCollider>(); } meshCollider.convex = true;
the main thing is that 'Collider' is not '$$anonymous$$eshCollider'. 'Collider' type has only shared properties for all type of colliders. '.convex' property is only $$anonymous$$eshCollider's property so 'Collider' doesn't have it. if you want to change this property, you must cast your collider instance to type of $$anonymous$$eshCollider.
'transform.collider' can be a box collider as well for example - what should do 'transform.collider.convex = true;' then?
ah lol okey...
I just didn'T get it that $$anonymous$$eshCollider is a component and I have to handle it like one, now it is clear.
But the problem that there is a short delay is still there..
any idea?
making mesh collider as convex needs some calculations. why don't you do it in editor? you can create convex collider in editor and just enable it at runtime
I tried that too.
It's about instantiating buildings in the game (like a strategy game) and everytime I turn on the convex it makes this delay.
So like you see, I cannot just put them in the scene before.
but I'm thinking about using only a box collider for the bottom of the building to be able to put it on a meshcollider surface and make the rest of the building as a trigger and make the actual collider effect with a script
Your answer
Follow this Question
Related Questions
Convex mesh collider thinks model is non manifold but it isn't 1 Answer
Covex Mesh Collider help 1 Answer
Creating more objects with code. 2 Answers
How to delay a respawn? 1 Answer