Before updating to Windows 10 you may need to return your Pro license. More information here

Rigidbody Bouncing Issue

I made a rigidbody character with the goal to have it "bounce" off of certain objects (basically deflect based on the angle of incidence and gravity). Someone on the forums gave me a small code to apply to the bounce object (a plane), which is as follows:

 var player : Transform;
 var bounceForce : float = 10;
 function OnCollisionEnter(other : Collision)
     player.rigidbody.AddRelativeForce(Vector3.up * 10, ForceMode.VelocityChange);

I noticed my character would be reflected only vertically, no matter what angle the object is. From some testing, I think the issue is that my character has rigidbody.FreezeRotation turned on, for obvious reasons.

Could anyone explain a way around this issue, either fixing the character end (rigidbody.FreezeRotation) or the bounce script? I have thought about using vector-based physics formulas to calculate the angle of reflection, velocity, etc. but at the moment I am not familiar enough to implement such a bounce script.


more ▼

asked Jun 22, 2011 at 12:37 AM

avatar image

217 15 12 15

Why aren't you just leaving the physics to Unity instead of coding it yourself?

Jun 22, 2011 at 12:45 AM Waz

What do you mean? The rigidbody.FreezeRotation? Or just using a rigidbody for the character in general? The latter is because it works better with other things such as moving platforms and pushing objects.

Jun 22, 2011 at 01:22 AM jesseoffy

The code you have says to thrust UP whenever you hit something. Where it says "Vector3.up" really means up. You could look at other.contacts[0].normal and thrust that way. But, easier to just add a physics material, set bouncy=1 and combine=max.

Jun 22, 2011 at 02:56 AM Owen Reynolds

Any of it. Unity can do all the bouncing you need just fine, you don't need to implement bouncing manually, just set the collider physics materials correctly.

Jun 22, 2011 at 05:21 AM Waz
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Edit 2: Solved with the kind help of [yakoma44][1]. Instead of moving the character using velocity and AddForce, I used MovePosition. So far, I haven't found any problems and the character doesn't seem to be doing any funky physics behaviors (fingers crossed). Here are the code changes sampled from my character script:


 var velocity = rigidbody.velocity;
 var velocityChange = (targetVelocity - velocity);    
 velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
 velocityChange.y = 0;
 rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);


 rigidbody.MovePosition(rigidbody.position + targetVelocity * Time.deltaTime);

The actual bounce force is applied on the bouncing collider: var bounceForce : float = 10;

 function OnCollisionEnter(other : Collision)
     other.rigidbody.velocity = transform.up * bounceForce;

Nota bene: AddForce or AddRelativeForce also work in the OnCollisionEnter, though the above is the shortest to write out. [1]: http://www.youtube.com/user/yakoma44[yakoma44][1]

more ▼

answered Jun 29, 2011 at 12:46 AM

avatar image

217 15 12 15

(comments are locked)
10|3000 characters needed characters left

I tried a rigidbody with freezed rotation and collider material set to Bouncy. When falling on some rigidbody, it didn't bounce at all. If falling on a collider without rigidbody, however, it bounced so endlessly that I had to define a damping factor in its script:

 var damping:float = 0.7;
 function OnCollisionEnter(){
     rigidbody.velocity.y *= damping;

It worked fine - at least for the bouncing effect. Hope it can solve your problem ;)

EDITED: Applied damping only to the Y velocity component. This allows easy control of how much the player bounce in different objects by altering the damping factor.
NOTE: damping is actually an energy conservation factor - it ranges from 0 (total damping) to 1 (no damping at all).

more ▼

answered Jun 22, 2011 at 02:43 AM

avatar image

61.6k 57 213 361

Collision results depend on the settings of BOTH colliders. Check them.

Jun 22, 2011 at 05:18 AM Waz

Why freeze rotation? If it's just for a visual reason then you can do that by tracking a visual object to an invisible physical object.

Jun 22, 2011 at 05:22 AM Waz

By collider material, you mean physic material, correct? I have played around with the physic materials, but those don't give me the control over the physics that I desire - being able to alter the amount of force that certain objects push up on the character. I have a few different techniques to test with - if I find a solution, I'll post it.

Jun 22, 2011 at 12:58 PM jesseoffy

Yes, I was talking about the physic material. I tried some combinations too, but the only one which made the character bounce like a crazy flea was Bouncy (character) + no rigidbody (platform). Other combinations gave results far away from what I expected. With the "damping" factor, I was able to control how much it bounced, but I have not tested other characteristics, like linear movement, collision with walls etc.
I also tried the brute force alternative, calculating it myself. I simply did this in the OnCollisionEnter event:

  rigidbody.velocity = Vector3.Reflect(rigidbody.velocity,hit.contacts[0].normal);

which should emulate a real bouncing, but it also gave strange results - maybe due to interaction with the physics engine.

Jun 22, 2011 at 06:39 PM aldonaletto

I admit it's late and I've spent hours staring at the screen trying to come up with other solutions (including returning to a CharacterController), and I think that somehow my controller stops the character from being able to "bounce" away from the object. It can only move vertically after it leaves the surface of the object. Edit: scripts removed.

Jun 23, 2011 at 02:05 AM jesseoffy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 22, 2011 at 12:37 AM

Seen: 10046 times

Last Updated: Jun 29, 2011 at 08:51 PM