Good evening everyone. As you can see from the image I would like to make sure that my car was able to travel this road by making this 360-degree turn while remaining glued to the asphalt. could anyone suggest me which approach to use and / or which script achieves this effect?
You can disable gravity in the car’s rigidbody and apply the gravity force via AddForce in the direction of the road.
rigidbody.AddForce(gravityDirection * 9.81f, ForceMode.Acceleration);
Here you would need to calculate gravityDirection as the normalized “down” direction with respect to the road.
PS. I love that stunt. The only game I know that includes it is the good old Fatal Racing for PC. I think it could be done without gravity tricks if the car travels it at enough speed in a precise direction. Testing it in Unity with a proper simulation is one of my pending tasks
How did you modeled the track? Some asset, or directly in a 3D tool?
Thank you so much for the advice. Having such a tight propeller and little space to apply it let’s say I can’t help but take the gravity out (even if the game has real physics). As for the track I started from these assets Simple Racer - Cartoon Assets | 3D Land | Unity Asset Store but I am doing a lot of things using Blender. For example, this screw is not part of the package and I made it myself with very little knowledge of Blender.
I finally solved the problem by writing this short script for my machine and I have to tell you that it works really well
void AttractorCheck()
{
int layerMask = 1 << 8;
layerMask = ~layerMask;
RaycastHit hit;
if (Physics.Raycast(RayCastTransf.position, transform.TransformDirection(Vector3.down), out hit, 5, layerMask) && hit.transform.CompareTag("AttractionLayer"))
{
Debug.DrawRay(RayCastTransf.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.yellow);
_rb.useGravity = false;
ApplyStabilizer(hit.normal);
_rb.AddRelativeForce(Vector3.down * 10000 * attractionSpeed * Time.fixedDeltaTime);
}
else
{
Debug.DrawRay(RayCastTransf.position, transform.TransformDirection(Vector3.down) * 5, Color.white);
_rb.useGravity = true;
ApplyStabilizer(Vector3.up);
}
}
If I can exploit your answer and this psot I want to ask you what is the best way for you to turn left and right with the car, I am having a lot of problems because I can’t find the nicest way to move the car. right now my approach is to evaluate a function based on horizontal key presses, only the effect is not that great. Do you have any proposals for scripts to use? I leave you below mine:
[SerializeField] AnimationCurve turnInputCurve = AnimationCurve.Linear(-1.0f, -0.3f, 1.0f, 0.3f);
void FixedUpdate()
{
steering = turnInputCurve.Evaluate(GetInput(turnInput)) * steerAngle;
// Direction
foreach (WheelCollider wheel in turnWheel)
{
wheel.steerAngle = Mathf.Lerp(wheel.steerAngle, steering, steerSpeed);
}
}
Thank you for the info!
Hint: you can replace “trasform.TransformDirection(Vector3.down)” with “-transform.up”.
I’d simply use:
wheel.steerAngle = Input.GetAxis("Horizontal");
Then I’d configure the Horizontal input in the Input Manager. Here’s a setup that works pretty good to me:
https://evp.vehiclephysics.com/faq/#how-to-configure-the-input-parameters
Nice!
I had some luck with a “magnetic flying cube” by changing the global Physics.gravity (in your case, I guess, to the normal of the centreline of the road):
Hmm, thinking about it, for proper glue, I guess maybe I’d need to increase the gravity magnitude too.
Well, you anyway have a solution and your game looks great:)
If anyone is here in the future, I wanted to share the package with the solution to the problem. All can be downloaded easily from my personal GitHub.