- Home /
Ricochet or Bounce Off Side Of Track
I modified the car tutorial so you drive a Toyota that doesn't stop accelerating. My problem is the car will get hung up on the rails or walls and keeps trying to drive into it. The player isn't able to reverse so I want to create a bounce or ricochet when the player hits the rails. The best example of what I want is how Cruis'n USA bounces the player off the side of the road.
The code that handles collosins only plays a sound right now:
var sound : SoundController;
sound = transform.root.GetComponent(SoundController);
private var car : Car;
car = transform.GetComponent(Car);
function OnCollisionEnter(collInfo : Collision)
{
if(enabled && collInfo.contacts.Length > 0)
{
var volumeFactor : float = Mathf.Clamp01(collInfo.relativeVelocity.magnitude * 0.08);
volumeFactor *= Mathf.Clamp01(0.3 + Mathf.Abs(Vector3.Dot(collInfo.relativeVelocity.normalized, collInfo.contacts[0].normal)));
volumeFactor = volumeFactor * 0.5 + 0.5;
sound.Crash(volumeFactor);
}
}
What sort of function can bounce the car off the edge of an object depending on what angle it is hit at?
Hi! Can you write your script, if you solve this problem? I've try make experiments with Reflect, but nothing gonna happens( I can't figure out how to use this function right to get ricoshet effect
Answer by BalsamicVinegar · Oct 26, 2011 at 06:24 AM
What exactly do you mean by stuck? Does the car 'stick' to the wall and you can't get off it? Because that sounds like a collider issue. If your wall collider is too thin and your car is moving fast enough, certain parts of the car might move through the entire collider before it is detected. Try making the wall collider thicker.
If that's not what you are asking, you could try the Vector3.Reflect function. This will reflect the movement vector in regards to the collision surface normal. So forward will still be forward but hitting when turing left should result in your car going right, giving you a bounce effect.
E.G.
rigidbody.velocity = Vector3.Reflect (rigidbody.velocity,collision.contacts[0].normal);
Answer by Crazy88 · Oct 27, 2011 at 08:02 PM
Thank you very much for replying E.G. The Vector3.Reflect function stops my car from getting hung up on walls, but it doesn't give the ricochet effect I need.
Instead of a car, let's pretend this is a bumper bowling game. With my current code, when the ball hits the bumper, it is sent back in the direction it came instead of of being deflected and keep moving forward.
Is there another function or value I could add to the function to get the effect I want?
private var car : Car;
car = transform.GetComponent(Car);
function OnCollisionEnter(collInfo : Collision)
{
if(collInfo.gameObject.tag == "Bouncer")
{
rigidbody.velocity = Vector3.Reflect(transform.position,collInfo.contacts[0].normal);
}
}
Your answer
Follow this Question
Related Questions
Unity2D Collision is bugged 0 Answers
Bounce Back After Collision 0 Answers
Collision misses - possibly not bullet through paper! (now with video) 1 Answer
Destroy Projectile on collision 3 Answers