How to shrink an object based on collision through python?

I would like to make a sensor object that shrinks when it collides with a wall. Then returns to its original scale when it stops colliding.
For example:

Something like an object that’s 5 units long on its Y scale and shrinking all the way down to 1 unit when colliding with a wall. Then returning to its original length when it stops colliding.

I have tried to code this but it keeps on jittering and won’t stop changing scale.

Hmmmm, someone else had a similar problem recently… let me see if I can find it.

There we go, let me know if that helps.

Thank you for trying to help me.
But the topic you’ve shown me shrinks the object by a keypress. What I wanted to achieve is, for the object to gradually shrink when pressing against the wall and then, for it to gradually return to its the original size when it would no longer collide with the wall.
I was able to code it but it keeps on jittering between sizes.

Hmmm… Is the collission detection necessary? It just seems like the wrong approach to me - but then I don’t know exactly what your usecase is. Could a raycast be used instead? I imagine the raycast would give you pixel-perfect scaling values, and therefore no jittering at all?

1 Like

this is pretty simple to make using animated shapekeys.

animated-shapekeys.blend (526.7 KB)
(Demo made in blender 2.79)

1 Like

I wanted to create a sensor for the camera but a ray sensor did the job, thanks.

Thank you for the demo. In the end, I have decided to use something else but this might be helpful in the future.