Search Unity

Having trouble implementing raycast suspension.

Discussion in 'Physics' started by BruceKristelijn, May 20, 2019.

  1. BruceKristelijn

    BruceKristelijn

    Joined:
    Apr 28, 2017
    Posts:
    108
    Hello! I am trying to implement a custom solution for wheels. I am basically trying to replicate this:


    I am having issues replicating the suspension as explained in the video. My car just bounces up and down., stays down or just does nothing. My current scripts work as follows:

    This is the CarWheel script. It applies the force at the desired locations.
    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         RaycastHit hit;
    4.         if (Physics.Raycast(transform.position, -transform.up, out hit, carController.suspensionHeight))
    5.         {
    6.             Debug.DrawRay(transform.position, -transform.up * hit.distance, Color.red);
    7.  
    8.             compression = Mathf.InverseLerp(carController.suspensionHeight, 0, hit.distance);
    9.             compression = Mathf.Clamp(compression, 0, 1);
    10.         }
    11.  
    12.         isGrounded = compression != 0;
    13.  
    14.         Vector3 appliedHoverForce = transform.up * carController.suspensionForce * compression;
    15.         carController.rigidbody.AddForceAtPosition(appliedHoverForce, transform.position);
    16.     }
    These are the variables on the parent script.
    upload_2019-5-20_17-11-27.png

    Is there anyone which might have an idea how to achieve the desired effect? I ma kinda stuck and can't seem to figure it out.
     
  2. radialxawn

    radialxawn

    Joined:
    May 13, 2019
    Posts:
    29
    Try this code, i tested.
     

    Attached Files:

  3. radialxawn

    radialxawn

    Joined:
    May 13, 2019
    Posts:
    29