shaking script get error, need help

i just start to use script in unity, my question is very simple: i want make something shaking with the Code:

var smooth = 1.0;
var chaos = 1.0;
function Update () {
transform.position = Vector3.Lerp(transform.position, Random.insideUnitSphere*chaos, Time.deltaTime * smooth);
}

when i put the code to a ball, it can shake but automatically move to the world 0,0,0 and shake there…I want it stay in its original place , how do I do? :roll:

Try this:

// just the position changing part
transform.position= transform.position + Vector3.Lerp(transform.position, Random.insideUnitSphere*chaos, Time.deltaTime*smooth);

The difference between this and your base code is that this takes the current position into account, vs just the origin (transform.position + …)

thanks a lot, but with code above, the ball is very angry and fly away…

I begin to realize that i should replace the world coordinate to local coordinate , but I don’t know how to do , maybe here is unsuitable to use vector3 ?

you have to use transform.localPosition
and in your scene tree, make an empty transform that defines the world position, and as child your ball.
if you modify the localPosition it will stay at the worldPositon + your local random position.