- Home /
Scaling object in Update() causes significant frame rate issues
Basically I'm trying to make a particular object scale based on its distance from the camera, which means I have to scale it in Update(). Here is a pic to show what I'm doing: http://i.imgur.com/83d1aeP.png
The problem is that when I scale this object, it also scales the colliders which kills performance. The profiler tells me "Mesh.Bake Scaled Mesh PhysX Collision Data" is taking up 76.8% of processing time (~2000 fps -> ~200 fps with v-sync off).
Is there any way to scale the MeshFilters only and not the colliders? Or is there a way I can scale the vertices on the MeshFilters? The object is composed of multiple filters/colliders so I can't really just scale the mesh.vertices for each MeshFilter (they will scale relative to their own centers/pivot points, not relative to that grey center dot in the pic above).
I still need the MeshCollider to be available and I would just update it before doing any ray casting so I can't just delete it. If anyone can help me out then I'd really appreciate it.
Here's my current code for scaling:
Vector3 distance = transform.position - Camera.main.transform.position;
transform.localScale = Vector3.one * 0.03f * Vector3.Dot(distance, Camera.main.transform.forward);
Wouldn't you be better off using another camera to render that object at the same location as the world x/y coordinates converted to the camera's screen coordinates? That way it wouldn't have to change size...
I need the collider because I'm doing ray casts on it. If you look at the picture (http://i.imgur.com/83d1aeP.png), you will see some arrows which are all selectable if you click them.
Whydoidoit, that solution seems great but how exactly would I go about doing it?
Alright, I'll try that out. I've never really messed with cameras like this before. Just so you guys are clear, I'm making something very similar to how the built in move tool works in unity. I appreciate the help and I'll let you guys know how it goes.
I've been messing with colliders a lot today and I managed to get some pretty good results. Scaling Box/Capsule colliders every frame isn't nearly as bad as scaling $$anonymous$$eshColliders, but I managed to get even better fps using a similar method to what I did with the $$anonymous$$eshColliders. I basically just add a new Box/Capsule Collider component to the object right before selection and delete the component after selection/raycasting. Here's the code for creating a collider for one of the arrows:
CapsuleCollider xAxisCollider =
GameObject.Find("TransformAxisX").AddComponent<CapsuleCollider>();
xAxisCollider.radius = 0.05f;
xAxisCollider.height = 1.2f;
xAxisCollider.direction = 0;
This code barely even makes a mark in the profiler.
So in the end, I get about 2300 fps while the objects are being rendered which is much better than before. I really appreciate the advice on using box/capsule colliders since it definitely helped performance a lot.
With the create/destroy collider method, I get ~2300 fps opposed to ~1900 fps with just constantly scaling the colliders, so I think it was worth it. $$anonymous$$y game has some pretty heavy math calculations running every frame so it's nice to have some extra cpu power available.
Answer by Fattie · Mar 28, 2013 at 08:01 AM
1 - Only scale the collider (say) once every second, or, indeed only scale it "on demand". (i.e.: Keep a flag whether it's been scaled or not, and only do so when you need it.)
2 - Don't use a mesh collider. In a word, they are almost never used in games. They are never used in UI.
Alright, but anyone with similar issues reading this should also read all of the comments to the first post.
particularly for the excellent statistics you posted, thanks for that
Answer by Paulius-Liekis · Apr 03, 2013 at 09:19 AM
Don't use mesh collider. If it's a sphere then use sphere collider :)
I assume you need collider only when you're close. Maybe disabling the collider will stop it from being scaled?
Put Collider and MeshRender on different GameObjects and scale only renderer.