- Home /
Throw car out of the circuit, based on its facing direction using physics
I was working on Car Wars game so I require to throw the car which crosses circuit boundary.
The following image depicts my point clearly: Now as per my programming setup, I require to throw my car through force/velocity manually applied and I want to flip the car in the car throwing direction.
I have tried myself about this but didn't able to get the desired result.
myRigidbody.AddForce(transform.forward * 80f);
Vector3 currentRotation = transform.eulerAngles.normalized;
myRigidbody.angularVelocity = new Vector3(currentRotation.x * Random.Range(10f, 40f), Random.Range(10f, 20f), currentRotation.z * Random.Range(10f, 20f));
Now share your side suggestion regarding this, I want to give force and angular velocity manually based on side of the circuit car diving.
hello, have you tried getting the vector from the car to the center of the circuit, and applying the force in that direction?
Can you able to provide me some code to test for this? I will check on the spot and reply back to you. $$anonymous$$aybe you are in the right direction...
Vector3 direction = new Vector3(circuit.position - transform.position);
myRigidbody.AddForce(direction * 80f);
myRigidbody.angularVelocity = new Vector(direction * Random.Range(10f, 40f));
Answer by JonPQ · Feb 01, 2019 at 05:04 PM
you might want to add force in the direction of the edge of the track's normal vector, not the forward vector of the car... Or at least use the car's velocity vector perhaps. but track normal might be more consistent ? the car may be driving sideways or backwards, or at a glancing angle... That is pretty easy to calculate... if in square center postion of track... force vector is just left or right... If in the end (curved sections) its just a vector from the center of the circle towards car's current position. (normalized)
And to make the car rotate... you probably don't need to alter the angular momentum... just apply your push force at a point above the center of gravity of the car, and you'll naturally get rotation also... try using the car's position or center of mass for target position, plus Vector3.up * heightOffset. Then apply the force there... using Rigidbody.AddForceAtPosition
@JonPQ thanks for your above answer - I started working again on this point today. Can you write the above answer more properly? if become possible then please add small code snippets.