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
1
Question by RARoadkill · Nov 22, 2013 at 03:41 AM · physicsreflectbouncing

Issue using Vector3.Reflect to bounce a ball

Hello everyone, having a rather annoying issue. Setting up a simple ball bouncing game, set up some code to have the ball reflect off a surface. It works great on my 4 walls, but it will not reflect off my paddle. Reflects off the walls, hits the paddle, then it just stops. I have gotten rid of any code that could be causing this, even turned the paddle into a simple cube and it still happens.

This is the code that is attached to the bouncing balls that causes the reflection

 void OnCollisionEnter(Collision collisionInfo)
     {
 
         rigidbody.AddForce(Vector3.Reflect(transform.position, 
                            collisionInfo.contacts[0].normal) * reflectSpeed, ForceMode.Impulse);//this causes the ball to reflect off the surface
         transform.rotation = Quaternion.LookRotation(rigidbody.velocity);//this rotates the ball to face in the direction it is moving
 
     } 

Kind of at a loss with this one. The paddle is not a rigidbody, the balls are. The only script attached to the paddle makes it rotate around a point, and I even turned that off and it made no difference.

Here is a link to a cut down to the bones version of what I am doing, just shows the bouncing issue.

Bare Bones

Thanks!

Comment
Add comment · 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 iwaldrop · Nov 22, 2013 at 07:30 AM 0
Share

The paddles should be as well, if for no other reason than performance. It's cheaper to move rigidbodies around than to move GameObjects with only colliders attached. Also, is it necessary for the ball to be facing in a particular direction if physics is driving it? Just wondering, because a) I'm crazy and b) it seems unnecessary (but I'm probably missing something).

In the interest of furthering this question toward an answer, are you sure this method is being called? It looks good at first glance.

avatar image Benproductions1 · Nov 22, 2013 at 07:43 AM 0
Share

You shouldn't reflect the position (Since it's a position), but the velocity

avatar image RARoadkill · Nov 22, 2013 at 02:50 PM 0
Share

so you are saying on my Vector3.Reflect I should be using rigidbody.velocity ins$$anonymous$$d of transform.position? I will give that a shot when I get home, but I am not sure why that would cause it to not reflect off that one surface.

In the full version of the game I am working on there is a case where I need to add force in the direction the ball is moving. So I just rotate it on collision to keep track of it. I did add a debug too that function it got called whenever the balls hit the paddle

avatar image RARoadkill · Nov 25, 2013 at 09:17 PM 0
Share

ugh sorry about the delay. I still have had no luck with this issue. I tried using velocity but it it not help. It changed it but the reflection was all weird. Bouncing off in odd directions. anyone have any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by kiritaku · Nov 25, 2013 at 10:00 PM

Update: this is probably easiest to do by adding a Physic Material (create Physic Material in assets window) to your ball, and setting Bounciness to 1 and Bounce Combine to Maximum. that gets it bouncing properly. if you need the bumper effect from pinball machines, then you could use something like:

 void OnCollisionEnter (Collision c) {
         // bumper effect to speed up ball
         myRigidbody.AddForce(c.contacts[0].normal*bumperForce, ForceMode.Impulse);
     }

if you want to bypass the physics engine's collision and do your own then personally i wouldn't use addforce, i would just directly mess with the velocity. or maybe just use addforce on the "bumper" part so that bouncing objects of different masses would get bounced differently.

also i tested why you were having issues, and i believe it is because the physics engine is handling the collision and killing the ball's velocity before you get your hands on it. to work around that i continually save the velocity. maybe there is a way to tell the physics engine not to actually perform the collision which would be much neater, but this works :)

i included 2 ways of bouncing, and added a "bumper boost" that it seems like you wanted... if you want to do it with multiplication instead of addition that should be pretty straight forward too.

 using UnityEngine;
 using System.Collections;
 
 public class ballScript : MonoBehaviour {
     
     Rigidbody myRigidbody;
     Vector3 oldVel;
 
     void Start () {
         myRigidbody = GetComponent<Rigidbody>();
     }
     
     void FixedUpdate() {
         oldVel = myRigidbody.velocity;
     }
     
     void OnCollisionEnter (Collision c) {
         ContactPoint cp = c.contacts[0];
         // calculate with addition of normal vector
         // myRigidbody.velocity = oldVel + cp.normal*2.0f*oldVel.magnitude;
         
         // calculate with Vector3.Reflect
         myRigidbody.velocity = Vector3.Reflect(oldVel,cp.normal);
         
         // bumper effect to speed up ball
         myRigidbody.velocity += cp.normal*2.0f;
     }
 }
 
Comment
Add comment · Show 4 · 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 RARoadkill · Nov 25, 2013 at 10:05 PM 0
Share

That looks great. I will be home to try that out here soon. Thanks for the reply!

avatar image kiritaku · Nov 25, 2013 at 10:08 PM 0
Share

np, lemme know if it fixes your issue.

avatar image kiritaku · Nov 25, 2013 at 11:27 PM 0
Share

ran into the way to do this using the physics engine, so posted that at the top :)

avatar image RARoadkill · Nov 26, 2013 at 01:36 AM 0
Share

Just tried it. It works perfectly. Thank you very much.

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making an object bounce off a wall the same way light bounces off of a mirror. 4 Answers

Parenting player to car on click. 1 Answer

Make car CenterOfMass change dynamically 1 Answer

If statment to check if player is within collider 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