Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Essential · Sep 19, 2013 at 06:07 PM · functionitweenbounce

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Essential · Sep 20, 2013 at 12:54 AM 0
Share

Wow Robert. Works incredibly well, and so simple. Perfect, thanks.

avatar image Essential · Sep 20, 2013 at 02:37 AM 1
Share

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;
     }
 }
avatar image siddharth3322 Essential · Oct 27, 2018 at 04:07 PM 0
Share

@Essential thank you for this awesome update :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a object bounce ("U" shape) by using Itween? 0 Answers

iTween - calling functions on oncomplete doesn't work if the function is declared as a variable 1 Answer

How do you stop a script executing mid-function? 4 Answers

Controlling iTween Overshoot Amount 1 Answer

Function after iTween animation finished 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges