Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by supratroopa · Mar 27, 2014 at 08:45 PM · rotationcollisionreflectionbounce

Problem with reflection against rigidbody

I have an object that moves based on AddRelativeForce in a given direction. The object changes direction slightly and the force is added via FixedUpdate(). Now, I want to make this object change direction when it hits a wall. Put shortly, I want the object to change direction in a way that perfectly mirrors the direction of the collision, like this. The FixedUpdate() function would take care of moving the object in this new direction.

So far, I have this code:

 private int count;
 public int moveSpeed = 500;
 public float maxVelocity = 10.0f;
 private float lookSpeed = 15.0f;
 // rotation variables
 private float rotationX = 0.0f;
 private float rotationY = 0.0f;


 void Start () 
 {
     count = 0;
 }



 void FixedUpdate () 
 {
     //alter direction slightly every 5 counts
     if (count == 5) 
     {
         rotationX += Random.Range (-0.2f, 0.2f) * lookSpeed;
         rotationY += Random.Range (-0.2f, 0.2f) * lookSpeed;
         rotationY = Mathf.Clamp (rotationY, -90, 90);

         transform.localRotation = Quaternion.AngleAxis (rotationX, Vector3.up);
         transform.localRotation *= Quaternion.AngleAxis (rotationY, Vector3.left);

         count = 0;
     }

     count++;

     //apply force in given direction, with limit
     if (rigidbody.velocity.magnitude > maxVelocity)
     {
         rigidbody.velocity = rigidbody.velocity.normalized * maxVelocity;
     }    
     
     rigidbody.AddRelativeForce(moveSpeed * Vector3.forward);
 }



 //reflect off wall
 void OnCollisionEnter(Collision collision)
 {
     if (collision.collider.tag == "wall") {
         ContactPoint hit = collision.contacts[0];
         Vector3 reflection = Vector3.Reflect (rigidbody.velocity, hit.normal);
         rotationX = reflection.x;
         rotationY = reflection.y;
     }
 }







As it stands, however, the object tends to turn in the direction of the object it is colliding with. i.e.: it's running right into it.

Is there something wrong with my code?

Thanks in advance!

Comment
Add comment · Show 5
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 Josh707 · Mar 27, 2014 at 09:32 PM 0
Share

I think you will want to set the rigidbody's velocity to the reflected vector that you're calculating, right now it seems you're just changing the rotation of the object.

 rigidbody.velocity = Vector3.Reflect(rigidbody.velocity, hit.normal);

That should behave as expected, if you want the rotation to align to it as well as reflect the velocity you can use LookRotation and the normal direction. Depending on your model you might want to reverse the normal direction (-hit.normal).

 transform.rotation = Quaternion.LookRotation(hit.normal);
avatar image supratroopa · Mar 27, 2014 at 09:42 PM 0
Share

Thanks for the reply. The reason I didn't want to change the velocity in this function is that rotationX and rotationY are global variables that change the direction of the object after which AddRelativeForce takes care of the velocity in FixedUpdate.

I changed my code to:

 if (collision.collider.tag == "wall") {
             Vector3 oldVelocity = rigidbody.velocity;
             ContactPoint hit = collision.contacts[0];
             Vector3 reflection = Vector3.Reflect (oldVelocity, hit.normal);
             rigidbody.velocity = reflection;
             Quaternion rotation = Quaternion.FromToRotation (oldVelocity, reflection);
             transform.rotation = rotation * transform.rotation;
             rotationX = transform.rotation.x;
             rotationY = transform.rotation.y;
     }

It seems to handle certain wide angle collisions O$$anonymous$$, but still gets stuck when hitting a wall perpendicularly. Any suggestions?

avatar image Josh707 · Mar 27, 2014 at 10:47 PM 0
Share

Hmm... Yeah I was thinking that setting its rotation could possibly shove some parts of the model inside the surface. I'm not exactly sure why it would get stuck though.

Perhaps you can completely replace the reflection with a physic material with a high bounciness setting? If not maybe you can move the object in the normal direction until it's outside the surface? I'm not sure about the best way to do that though, maybe testing bounds intersection...

avatar image supratroopa · Mar 28, 2014 at 01:13 AM 0
Share

I'm kinda revisiting the way the fish moves. Is there a way to simply edit rigidbody.velocity directly, ins$$anonymous$$d of having addrelativeforce? I'm trying but it's giving me some weird results.

avatar image warren- · Jan 28, 2016 at 05:41 PM 0
Share

you might also want to check the "Default Contact Offset" located in Edit->Project Settings->Physics

This solved one of my reflection problems, when my objects velocity was increased. The objects would scatter in unpredictable ways until I reset it from .01 to .06

This creates a larger 'hotspot' for the collision detector, so that your object doesn't touch the object its colliding with.

A higher "Solver Iteration Count" is supposed to give you higher resolution collisions as well, although I couldn't tell much difference, and made things much slower.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xainulabdeen · Nov 19, 2018 at 12:15 PM

try this Bounce Reflection

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get angle to retarget collision object. 1 Answer

Bounce when hitting wall 2 Answers

Player rotating after wall collision 1 Answer

Questions about rotation. 1 Answer

Unrealistic bounce 2 Answers


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