Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Magnet street - How car object can follow the street in real world physic

Discussion in 'Physics' started by ArcaDone, Mar 19, 2021.

  1. ArcaDone

    ArcaDone

    Joined:
    Jan 6, 2021
    Posts:
    10
    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?
    Schermata 2021-03-18 alle 18.44.37.png
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    You can disable gravity in the car's rigidbody and apply the gravity force via AddForce in the direction of the road.
    Code (CSharp):
    1. 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?
     
    ArcaDone likes this.
  3. ArcaDone

    ArcaDone

    Joined:
    Jan 6, 2021
    Posts:
    10
    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 https://assetstore.unity.com/packages/3d/vehicles/land/simple-racer-cartoon-assets-37490 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 :)

    Code (CSharp):
    1.     void AttractorCheck()
    2.     {
    3.         int layerMask = 1 << 8;
    4.  
    5.         layerMask = ~layerMask;
    6.  
    7.         RaycastHit hit;
    8.         if (Physics.Raycast(RayCastTransf.position, transform.TransformDirection(Vector3.down), out hit, 5, layerMask) && hit.transform.CompareTag("AttractionLayer"))
    9.         {
    10.             Debug.DrawRay(RayCastTransf.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.yellow);
    11.             _rb.useGravity = false;
    12.             ApplyStabilizer(hit.normal);
    13.             _rb.AddRelativeForce(Vector3.down * 10000 * attractionSpeed * Time.fixedDeltaTime);
    14.  
    15.         }
    16.         else
    17.         {
    18.             Debug.DrawRay(RayCastTransf.position, transform.TransformDirection(Vector3.down) * 5, Color.white);
    19.             _rb.useGravity = true;
    20.             ApplyStabilizer(Vector3.up);
    21.         }
    22.     }
     
  4. ArcaDone

    ArcaDone

    Joined:
    Jan 6, 2021
    Posts:
    10
    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:

    Code (CSharp):
    1. [SerializeField] AnimationCurve turnInputCurve = AnimationCurve.Linear(-1.0f, -0.3f, 1.0f, 0.3f);
    2.  
    3.     void FixedUpdate()
    4.     {
    5. steering = turnInputCurve.Evaluate(GetInput(turnInput)) * steerAngle;
    6.  
    7.         // Direction
    8.         foreach (WheelCollider wheel in turnWheel)
    9.         {
    10.             wheel.steerAngle = Mathf.Lerp(wheel.steerAngle, steering, steerSpeed);
    11.         }
    12.  
    13. }
     
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    Thank you for the info!

    Hint: you can replace "trasform.TransformDirection(Vector3.down)" with "-transform.up".

    I'd simply use:
    Code (CSharp):
    1. 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
     
    ArcaDone likes this.
  6. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    28
    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):
    https://twitter.com/biscuitsmitten/status/1309220478255497216
    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:)
     
    ArcaDone likes this.
  7. ArcaDone

    ArcaDone

    Joined:
    Jan 6, 2021
    Posts:
    10
    codebiscuits likes this.