- Home /
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.
You could just apply a bouncy physics material.... it's in one of the packages.
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
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.
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!
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?
Eric5h5 You are awesome, Thanks for all the help you have given.
@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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082923im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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