How to make a sphere bounce while it is moving forwards

Updated on July 7, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on June 30, 2019

Hello,

I am trying to make a sphere that I have moving forwards also bounce at the same time. Here is what I have tried.

Adding a physic material to the sphere and setting the material with a friction combine of minimum, setting the bounce combine to the maximum.

Setting the collision detection on the rigidbody of the sphere to continuous.

After trying all of this, it all resulted in when the sphere hits the other gameobject just sitting there and moving forwards without bouncing. Here is the code that I have for moving the sphere.

 

public float forceMulti = 50f;

private Rigidbody rb;

private void Awake()
{
rb = GetComponent<Rigidbody>();
}

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
rb.velocity = transform.forward * Time.deltaTime * forceMulti;
}

 

If there is a way to make the sphere bounce through code while having it move forwards that would be great and I would really appreciate the suggestions.

 

Thanks!

  • Liked by
Reply
4 on July 1, 2019

Ok, so there are 2 (technically 3) things to look at here.

First: Maximum isn’t actually the highest you can go with physics materials. Maximum just means it will only look at the bigger of the 2 numbers for the colliding RigidBodies. Multiply is actually the highest (assuming both numbers are greater than 1)

Second: You’re directly setting the velocity in the update loop. Traditionally you would want to use something like AddForce. By directly setting the velocity, you’re overriding the whatever the physics engine is trying to do to the body. So it may be trying to bounce in one direction, but you’re resetting it every frame.

Third (kinda): Do physics stuff in FixedUpdate, instead of Update.

 

Without changing anything else, try this instead of your current Update loop, and see what happens.

void FixedUpdate()
{
     rb.AddForce(Vector3.forward * forceMulti);
}
on July 3, 2019

Thanks for the reply. I did what you suggested and it has brought back the bouncing. Now what I am noticing is that the ball bounce height seems to increase over time. Is there a way to keep that constant? I tried moving the rb.addforce line to the start method and that slows the ball down over time. I also tried adding a material to the surface that the ball is bouncing on with zero friction and nothing changed. If you have any suggestions that would be great!

 

Thanks!

Wise
on July 3, 2019

Moving the add force to the start will mean the ball will only get it once. It being in update means the force added constantly.

For the bouncing higher, try setting the physics material to ‘Average’ instead of multiply. The bounce may be too much, and is pushing the ball higher than it started, causing it to fall harder, and the cycle repeats.

on July 7, 2019

I have tried setting the material to Average but all that does is cause the ball to bounce once then never again. The only one that makes the ball bounce is maximum. And overtime on that setting the balls bounce heigh and speed increase over time. I also tried setting it on the other settings and that also resulted in the ball bouncing once then never again. If you have any other suggestions please let me know.

 

Thanks 

on July 7, 2019

I was able to fix it! I just used on OnCollsionEnter method to switch the direction of the addforce when the player comes into contact with the ground. 

 

Thanks so much for your help.

Show more replies
  • Liked by
Reply
Cancel