I am trying to make a ball react to a wall and other objects.
function OnCollisionEnter(collision : Collision){
var newVelocity;
var normalHit = collision.contacts[0].normal;
newVelocity = rigidbody.velocity + normalHit;
var newVelocity1;
newVelocity1 = Vector3(newVelocity) * Vector3(normalHit);
var newVelocity2;
newVelocity2 = Vector3.Dot(velocityMultiplier, newVelocity);
var newVelocity3;
newVelocity3 = newVelocity2 - rigidbody.velocity;
rigidbody.velocity = newVelocity3;
}
My main theory is one i found on here earlier…
Velocity New = 2 * (velocity+normalVector) * normalVector - velocity
Ok so I have most of this working, i have gotten the ball to bounce a couple of times. When it bounces however it is getting faster, which is cool but is causing clipping issues after a certain point. The ball only bounces in the Y plane, and when the ball hits an X plane barrier it passes through it.
Heres my bounce code for the ball:
function OnCollisionStay(collision : Collision){
print("I hit");
print(rigidbody.velocity);
var reflection;
var myVelocity;
myVelocity = rigidbody.velocity;
var hitNormal = collision.contacts[0].normal;
reflection = -2 * (Vector3.Dot(myVelocity, hitNormal)) * hitNormal - myVelocity;
rigidbody.velocity = reflection;
print(rigidbody.velocity);
}
Any thoughts?
Why are you trying to program in basic physics collision reactions when they are programmed in?
Can’t you create a physics material and set bounciness, etc and let the PhysX engine handle it?
Perhaps try this:
reflection = -2 * (Vector3.Dot(myVelocity.normalized, hitNormal)) * hitNormal - myVelocity.normalized;
rigidbody.velocity = reflection * myVelocity.magnitude;
If you want the ball to get slower with each impact, do…
rigidbody.velocity = reflection * myVelocity.magnitude * dampFactor;
Where dampFactor is between [0,1] if you want the ball to get slower, or [1,Infinity] if you want it to get faster. Also, make sure your rigid body is set to CONTINUOUS.