- Home /
How should I scale an object with collider?
Hi,
I currently a have randomly generating world consisting of simple blocks with colliders. When these blocks come inside the player's vision range, they are scaled with Lerp from 0,0,0 to 1,1,1 in real time currently with this code:
thisBlock.transform.localScale = Vector3.Lerp (thisBlock.transform.localScale, maxScale, speed * Time.deltaTime); speed = speed + 0.1f;
As the profiler picture below shows, the method is not very efficient and it's giving me these errors apparently because the blocks have colliders? And I read that scaling with colliders is not a good idea.
I was wondering would it be any lighter/better idea in general to make animations of my cubes and all other objects, scaling from 0,0,0 to 1,1,1 and then play these animations when the blocks come in the range? All goal scales would be predefined, such as from 0,0,0 to 1,1,1 and back to 0,0,0. Could I make these kind of animations with animator + Mecanim? The blocks have no bones.
Or would there be some other even better way?
I found out that I should start by separating the collider to a child object, which should stop the errors.
Edit: Collider moved to child and a test with mechanim animation on the parent still causes warnings through those colliders on child. Sigh.. Can I somehow record an animation to scale only in local scale?
Edit2: I suppose I need even more children. Perhaps: 1. GrassBlock (just empty) 1.1. $$anonymous$$esh (with the mesh, texture and animator) 1.2. Collider (with boxcollider and script handling collisions)
But that's again so much refactoring that I'll have to try it tomorrow :)
Answer by Morvar · Feb 20, 2014 at 02:47 PM
After 17 hours as there's been no better alternative suggestions I'll mark my mecanim animation as a solution. It might not be the most efficient way to scale but at least with the 1, 1.1, 1.2 structure I also got rid of the errors related to colliders being scaled.