-1
\$\begingroup\$

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 it is moving forwards that would be great and I would really appreciate the suggestions.

Thanks.

\$\endgroup\$
  • \$\begingroup\$ You are setting the velocity every frame in Update. So any vertical bouncing movement it might have wanted to do, you're deliberately overriding and telling it "no, DO NOT bounce, just move forward and only forward instead". \$\endgroup\$ – DMGregory Jun 30 at 20:11
  • \$\begingroup\$ The suggested answer below has brought back the jumping on the sphere. The problem now is that over time the height of the jump, as well as the speed of the sphere, increases over time. Is there a way to keep both of those constant? Thanks! \$\endgroup\$ – Cole.C Jul 1 at 0:06
0
\$\begingroup\$

Don't directly set the velocity of the rigidbody if you want other physics behaviors. Use rb.AddForce instead.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the reply. I have changed the line to this. rb.AddForce(Vector3.forward * Time.deltaTime * forceMulti); And this has seemed to have brought back the jumping. But the problem now is that the height of the jump seems to increase over time. As well as the speed of the sphere. Is there a way to keep the speed and the height of the jump constant over time? Thanks! \$\endgroup\$ – Cole.C Jul 1 at 0:02
  • \$\begingroup\$ You should only need to call AddForce once. Think of it as giving the sphere a push - when you replaced the line, you're now pushing the sphere once each frame, increasing the speed over time. Try moving the AddForce line into Start. \$\endgroup\$ – Alex F Jul 1 at 5:16
  • \$\begingroup\$ Thanks for the suggestion. I moved the addforce line to the start method and now the ball slows down over time. I also tried adding a material to the surface that the ball is bouncing on with zero friction and that changed nothing. If you have any other suggestions that would be great! \$\endgroup\$ – Cole.C Jul 3 at 0:36
  • \$\begingroup\$ To clarify, your goal is to have the ball bounce while moving at a constant speed along the z-axis? \$\endgroup\$ – Alex F Jul 3 at 5:19
  • \$\begingroup\$ Yes that is correct. \$\endgroup\$ – Cole.C Jul 3 at 14:36
0
\$\begingroup\$

If your player don't change height in the game then how about simply animating the sphere to bounce while moving forward? no need to go complex.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.