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 pepperedereppep · Nov 13, 2010 at 02:26 AM · animationballbounce

bouncing ball without physics/gravity

Hey, another quick question here from a beginner. II'm playing around with code and right now I'm trying to figure out how to create a bouncing ball without using any physics/force/gravity. I'm basically trying to learn how this kinda coding works so I can apply it to other things that aren't exactly meant to be physics based.. here it is so far(not very far :)):

var YMovement : float = 0; var damp:float; var fall:float; var jump:boolean=false;

function Update () {
if(jump==false){ YMovement = Mathf.Lerp(YMovement, -1, Time.deltaTime*damp); transform.Translate (0,YMovement 12 Time.deltaTime, 0); }else if(jump==true){ transform.position.y+=fall*Time.deltaTime; //??? } }
function OnTriggerEnter(col:Collider){ if(col.gameObject.tag=="ground"){ jump=true; } }

this code is attached to a box that would be bouncing on top of another box tagged as "ground". Right now after the bounce all I have is the ball shooting towards the positive y axis, but the initial fall is the way I want it ..Any pointer on how to do this or if I'm even doing it right would be greatly appreciated.

Comment
Add comment · Show 2
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 ChimpKing · Nov 13, 2010 at 02:46 AM 0
Share

You could just apply a bouncy physics material.... it's in one of the packages.

avatar image pepperedereppep · Nov 13, 2010 at 03:15 AM 0
Share

Hey. As I said, I'm not using physics/gravity. I don't even intent to use this code for a ball bounce, I just want to know how one would go about writing a code like this

2 Replies

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

Answer by Eric5h5 · Nov 13, 2010 at 03:37 AM

To do it totally programmatically without physics, you can do something like this:

var bounceSpeed = 1.0; var bounceAmount = 2.0;

function Start () { while (true) { var t = 0.0; while (t < 1.0) { t += Time.deltaTime bounceSpeed; transform.position.y = Bounce(t) bounceAmount; yield; } } }

function Bounce (t : float) : float { return Mathf.Sin(Mathf.Clamp01(t) * Mathf.PI); }

Edit: actually you can remove the Start function and use this if you wanted the bouncing to continue forever:

function Update () {
    transform.position.y = Bounce((Time.time * bounceSpeed)%1) * bounceAmount;
}

If you wanted to limit the number of bounces, though, then keep the Start function and replace "while (true)" with "for (i = 0; i < 10; i++)" for 10 bounces, etc.

Comment
Add comment · Show 4 · 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 pepperedereppep · Nov 13, 2010 at 04:35 AM 0
Share

thanks! looks like what I want. Now I'm going to stare at the code for a while to figure out what's going on :) Thank you!

avatar image pepperedereppep · Nov 13, 2010 at 05:45 AM 0
Share

hey Eric, thanks a lot. I was wondering with that bounce function is it possible to have a trigger bounce the object back up rather than having it be based on time?

avatar image Maltus · Nov 13, 2010 at 05:50 AM 0
Share

Eric5h5 You are awesome, Thanks for all the help you have given.

avatar image Eric5h5 · Nov 13, 2010 at 06:29 AM 0
Share

@pepperedereppep: in order for triggers to work you need a rigidbody, in which case you might as well use physics/gravity to do the bouncing too.

avatar image
0

Answer by pepefirst · Jun 08, 2012 at 12:07 AM

This script is simply GREAT!

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animate a bouncing ball. 1 Answer

How to bounce a ball based on mouse click position? 0 Answers

Platform Bounce Physics (like Flabby Physics) 0 Answers

Block Braker with Raycasts 1 Answer

How to make a ball bounce when a user taps the screen ios 0 Answers


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