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?
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;
}
}