A trampoline reacts like a spring device. Let's assume gravity is in Y direction and the trampoline surface is positionied in the X,Z plane.
Then your Y coordinate y is proportional to a sine function during OnTriggerStay
. Velocity v in Y direction as 1st derivative of y is then a cosine function, while X and Z velocity remain constant.
y (t) = yMax * sin (f * t)
v (t) = yMax * f * cos (f * t)
Considering conservation of energy, we have:
E = 0.5 * m * vMax² = 0.5 * k * yMax²
=> yMax = ± SQRT (k / m) * vMax
- vMax := speed in Y direction when hitting the trampoline. ± because for landing and starting
- yMax := maximum amplitude when v == 0, i.e. hwo deep should the player sink before returning
- k := spring constant defining trampoline behaviour i.e. how strong it is
- m := player's mass
- f := SQRT (k / m)
So all you need to do is playing around with the spring constant and have something like this in your Update
method:
Vector3 velocity = rigidbody.velocity;
float elapsedTime = Time.time - timestampOnEnter;
velocity.y = YMax * FConst * Mathf.cos (FConst * elapsedTime);
rigidbody.velocity = velocity;
Member var timestampOnEnter
is taken in OnTriggerEnter
, FConst is the constant we called f in the maths part.