- Home /
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.
Thanks!
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.
You shouldn't reflect the position (Since it's a position), but the velocity
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
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?
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;
}
}
That looks great. I will be home to try that out here soon. Thanks for the reply!
ran into the way to do this using the physics engine, so posted that at the top :)
Just tried it. It works perfectly. Thank you very much.