• 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
Question by jesseoffy · Jun 22, 2011 at 12:37 AM · rigidbodybouncefreezerotation

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.

Thanks

Comment

People who like this

0 Show 4
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 Waz · Jun 22, 2011 at 12:45 AM 0
Share

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

avatar image jesseoffy · Jun 22, 2011 at 01:22 AM 0
Share

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.

avatar image Owen-Reynolds · Jun 22, 2011 at 02:56 AM 0
Share

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.

avatar image Waz · Jun 22, 2011 at 05:21 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by jesseoffy · Jun 29, 2011 at 12:46 AM

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:

Original

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

Fixed:

 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]

Comment

People who like this

0 Show 1 · 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 ocimum · Aug 14, 2015 at 01:22 PM 0
Share

whats the targetVelocity? its not defined as a variable

avatar image

Answer by aldonaletto · Jun 22, 2011 at 02:43 AM

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).

Comment

People who like this

0 Show 5 · 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 Waz · Jun 22, 2011 at 05:18 AM 0
Share

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

avatar image Waz · Jun 22, 2011 at 05:22 AM 0
Share

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.

avatar image jesseoffy · Jun 22, 2011 at 12:58 PM 0
Share

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.

avatar image aldonaletto · Jun 22, 2011 at 06:39 PM 0
Share

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.

avatar image jesseoffy · Jun 23, 2011 at 02:05 AM 0
Share

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.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

STAY STILL 2 Answers

Bounce physics is not working as expected in zero gravity. Is this right? 1 Answer

How to calculate a RigidBody2D's bounce from a collider? 1 Answer

How to make objects non-penetrable, inelastic 0 Answers

Player bouncing off issue 1 Answer


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