- Home /
How to create a bouncing effect
I have crates that the player can destroy and then it releases an item. I want to make the item bounce out from the crate.
I'm trying to avoid physics in my game as it's a mobile game. Instead I made a few attempts with iTween:
iTween.MoveFrom( orb, {"position":pos, "easetype":"EaseOutBounce", "time":1} ); // Bounce orb
This works but unfortunately bounces in all 3 dimensions, whereas I only want it to bounce in the Y axis. I also tried this:
iTween.MoveTo( orb, {"y":0, "easetype":"EaseOutBounce", "time":1} ); // Bounce orb
iTween.MoveFrom( orb, {"x":pos.x, "z":pos.z, "easetype":"EaseOutQuad", "time":1} );
This one doesn't seem to work as I think iTween can only apply one type of movement to an object at a time.
Is there another alternative. What's a way you guys would implement items bouncing into the scene in your game?
Answer by robertbu · Sep 19, 2013 at 09:35 PM
Here is a script implementing some very simple physics. It requires you set a floor height to bounce against. To 'fire' it, set the velocity to something other than zero, and/or move the object to a position above the floor. To test it, create a new scene, add a sphere, move the sphere above the floor height, add the script and run. You can also set the velocity in the inspector.
#pragma strict
var velocity = Vector3.zero;
var floorHeight = 0.0;
var sleepThreshold = 0.05;
var bounceCooef = 0.8;
var gravity = -9.8;
function FixedUpdate() {
if (velocity.magnitude > sleepThreshold || transform.position.y > floorHeight) {
velocity.y += gravity * Time.fixedDeltaTime;
}
transform.position += velocity * Time.fixedDeltaTime;
if (transform.position.y <= floorHeight) {
transform.position.y = floorHeight;
velocity.y = -velocity.y;
velocity *= bounceCooef;
}
}
Wow Robert. Works incredibly well, and so simple. Perfect, thanks.
Robert's script is great and there's not much I can do to improve it, but just in case someone else finds my version useful. This script will initially throw the object in the air and out in a random direction and then bring it down in a bounce. It is a bit more efficient and will stop checking after reaching the ground. Animation restarts when the GameObject is re-activated.
#pragma strict
private var tr : Transform;
private var velocity = Vector3.zero;
private var groundPos : float;
private var sleepThreshold = 0.0025;
private var bounceCooef = 0.6;
private var gravity = -9.8;
function OnEnable ()
{
groundPos = transform.localPosition.y;
tr = transform;
// Randomly rotate and then throw forward a random distance
transform.localRotation.y = Random.Range(0, 360);
velocity = tr.forward * Random.Range(0.5, 1.5);
// Throw upwards
velocity.y = 8;
Bouncy();
}
function Bouncy ()
{
while ( velocity.sqr$$anonymous$$agnitude > sleepThreshold )
{
if ( tr.localPosition.y > groundPos )
velocity.y += gravity * Time.deltaTime;
tr.position += velocity * Time.deltaTime;
if (tr.localPosition.y <= groundPos)
{
tr.localPosition.y = groundPos;
velocity.y = -velocity.y;
velocity *= bounceCooef;
}
yield;
}
}
@Essential thank you for this awesome update :)