Search Unity

Trying To Make Kart Based sunspension

Discussion in 'Scripting' started by isometricLove, Oct 13, 2014.

  1. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    Hi i am trying to make suspension like box shoud hower, now my isue is duno what probably bad formula for counter force or meybe beacuse i am not using FixedUpdate(it gives wierd lags) cuz i alwes get crazy result


    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class ReycastingSuspension : MonoBehaviour {
    7.     //The distance of ray
    8.     public float RaycastDistance;
    9.     //Variable to play whit in unity editor
    10.     public float SunspensionPower;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.                 //Difines botom corners so thet can later be used for raycasting
    20.                 Vector3 pointA = transform.TransformPoint (new Vector3 (0.5f, -0.5f, 0.5f));
    21.                 Vector3 pointB = transform.TransformPoint (new Vector3 (0.5f, -0.5f, -0.5f));
    22.                 Vector3 pointC = transform.TransformPoint (new Vector3 (-0.5f, -0.5f, 0.5f));
    23.                 Vector3 pointD = transform.TransformPoint (new Vector3 (-0.5f, -0.5f, -0.5f));
    24.        
    25.                 //defines HitColector and executes ray for PointA
    26.                 RaycastHit hitColectorPointA;
    27.                 if (Physics.Raycast (pointA, -transform.up, out hitColectorPointA, RaycastDistance)) {
    28.  
    29.                         //CRatio shoud be formula for how much ray is compresed
    30.                         float CRatio =  hitColectorPointA.distance / RaycastDistance;
    31.  
    32.                         //Add UP force at point to lift thet side if its lower by any other force
    33.                         rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointA, ForceMode.Force);
    34.  
    35.                         // GreenRay if hiting somthing
    36.                         Debug.DrawRay (pointA, -transform.up * RaycastDistance, Color.green);
    37.                         Debug.Log (hitColectorPointA.distance);
    38.                 } else
    39.                 {
    40.                         //Red Ray if not hiting anything
    41.                         Debug.DrawRay (pointA, -transform.up * RaycastDistance, Color.red);
    42.                 }
    43.  
    44.  
    45.                 //defines HitColector and executes ray for PointB
    46.                 RaycastHit hitColectorPointB;
    47.                 if (Physics.Raycast (pointB, -transform.up, out hitColectorPointB, RaycastDistance)) {
    48.  
    49.                         //CRatio shoud be formula for how much ray is compresed
    50.                         float CRatio =  hitColectorPointB.distance / RaycastDistance;
    51.  
    52.                         //Add UP force at point to lift thet side if its lower by any other force
    53.                         rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointB, ForceMode.Force);
    54.  
    55.                         // GreenRay if hiting somthing
    56.                         Debug.DrawRay (pointB, -transform.up * RaycastDistance, Color.green);
    57.                         Debug.Log (hitColectorPointB.distance);
    58.                 } else
    59.                 {
    60.                         //Red Ray if not hiting anything
    61.                         Debug.DrawRay (pointB, -transform.up * RaycastDistance, Color.red);
    62.    
    63.                 }
    64.  
    65.  
    66.  
    67.                 //defines HitColector and executes ray for PointC
    68.                 RaycastHit hitColectorPointC;
    69.                 if (Physics.Raycast (pointC, -transform.up, out hitColectorPointC, RaycastDistance)) {
    70.  
    71.                         //CRatio shoud be formula for how much ray is compresed
    72.                         float CRatio =  hitColectorPointC.distance / RaycastDistance;
    73.  
    74.                         //Add UP force at point to lift thet side if its lower by any other force
    75.                         rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointC, ForceMode.Force);
    76.  
    77.                         // GreenRay if hiting somthing
    78.                         Debug.DrawRay (pointC, -transform.up * RaycastDistance, Color.green);
    79.                         Debug.Log (hitColectorPointC.distance);
    80.                 } else
    81.                 {
    82.                         //Red Ray if not hiting anything
    83.                         Debug.DrawRay (pointC, -transform.up * RaycastDistance, Color.red);
    84.                 }
    85.  
    86.  
    87.  
    88.                 //defines HitColector and executes ray for PointD
    89.                 RaycastHit hitColectorPointD;
    90.                 if (Physics.Raycast (pointD, -transform.up, out hitColectorPointD, RaycastDistance)) {
    91.  
    92.                         //CRatio shoud be formula for how much ray is compresed
    93.                         float CRatio = hitColectorPointD.distance / RaycastDistance;
    94.  
    95.                         //Add UP force at point to lift thet side if its lower by any other force
    96.                         rigidbody.AddForceAtPosition (Vector3.up * CRatio * SunspensionPower, pointD, ForceMode.Force);
    97.  
    98.                         // GreenRay if hiting somthing
    99.                         Debug.DrawRay (pointD, -transform.up * RaycastDistance, Color.green);
    100.                         Debug.Log (hitColectorPointD.distance);
    101.                 } else
    102.                 {
    103.                         //Red Ray if not hiting anything
    104.                         Debug.DrawRay (pointD, -transform.up * RaycastDistance, Color.red);
    105.                 }
    106.     }
    107. }
    108. //Debug.LogWarning("WORKING");
    109.  
     
    Last edited: Oct 14, 2014
  2. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    How to appy FixedUpdate to my code but i need to smooth it whit deltaTime i dont get thet part
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Physics should always be called from "FixedUpdate":
    http://unity3d.com/learn/tutorials/modules/beginner/scripting/update-and-fixedupdate

    It's also worth looking at Physics in general:
    http://unity3d.com/learn/tutorials/modules/beginner/physics

    There are at least two approaches you can take to this:
    - One is to use forces, and you can look at the distance between the corner and the ground and the smaller the distance, the stronger the force.
    - Another is to use wheel colliders, but don't use any wheels. The colliders will be invisible, so you can make extra-soft wheel colliders and roll on them.

    One bit of advice when coding: If you ever repeat the same type of code more than once, you are doing something wrong.

    This code is inefficient:
    Code (CSharp):
    1.     public int a;
    2.     public int b;
    3.     public int c;
    4.     public int d;
    5.  
    6.     // Update is called once per frame
    7.     void Update () {
    8.         if (a > 0) {
    9.             float someValue = a * myCurrentValue;
    10.             float myRatio = someValue / someOtherValue;
    11.             myRigidbody.AddForce (myRatio * mySpeed);
    12.         };
    13.      
    14.         if (b > 0) {
    15.             float someValue = a * myCurrentValue;
    16.             float myRatio = someValue / someOtherValue;
    17.             myRigidbody.AddForce (myRatio * mySpeed);
    18.         };
    19.      
    20.         if (c > 0) {
    21.             float someValue = a * myCurrentValue;
    22.             float myRatio = someValue / someOtherValue;
    23.             myRigidbody.AddForce (myRatio * mySpeed);
    24.         };
    25.  
    26.         if (d > 0) {
    27.             float someValue = a * myCurrentValue;
    28.             float myRatio = someValue / someOtherValue;
    29.             myRigidbody.AddForce (myRatio * mySpeed);
    30.         };
    31.     }
    The first way to simplify things is to split our repetitive code into its own function:
    Code (CSharp):
    1.     public int a;
    2.     public int b;
    3.     public int c;
    4.     public int d;
    5.  
    6.     // Update is called once per frame
    7.     void Update () {
    8.         if (a > 0) {
    9.             DoSomething (a)
    10.         };
    11.      
    12.         if (b > 0) {
    13.             DoSomething (b)
    14.         };
    15.      
    16.         if (c > 0) {
    17.             DoSomething (c)
    18.         };
    19.  
    20.         if (d > 0) {
    21.             DoSomething (d)
    22.         };
    23.     }
    24.  
    25.     void DoSomething (int myInt) {
    26.         float someValue = myInt * myCurrentValue;
    27.         float myRatio = someValue / someOtherValue;
    28.         myRigidbody.AddForce (myRatio * mySpeed);
    29.     }
    The next thing to try is to simplifying the script by putting the values in a collection and the code in a loop:
    Code (CSharp):
    1.     public int[] myIntValues;
    2.  
    3.     // Update is called once per frame
    4.     void Update () {
    5.         foreach (int myValue in myIntValues) {
    6.             DoSomething (myValue);
    7.         }
    8.     }
    9.  
    10.     void DoSomething (int myInt) {
    11.         float someValue = myInt * myCurrentValue;
    12.         float myRatio = someValue / someOtherValue;
    13.         myRigidbody.AddForce (myRatio * mySpeed);
    14.     }
    15.  
     
  4. FuzzyBalls

    FuzzyBalls

    Joined:
    Jun 12, 2014
    Posts:
    47
    how would the vehicle smoothly stay at it's height? when I try this it bounces more like a a hovercraft rather than that video.
     
  5. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    i am trying to figure thet out too
     
  6. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    I know thet code was inefficient i just dident want to bother whit polishing it out before i figure out whats the issue i am trying to do first apprach but something is wrong whit my code dont know what i wated to get help about thet
     
  7. FuzzyBalls

    FuzzyBalls

    Joined:
    Jun 12, 2014
    Posts:
    47
    please post your progress if you figure it out :) thanks
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, I've not looked closely at the code, but in general - a hovercar should float or bounce a bit, as it's not on wheels. So, forces can give that floaty feeling. If it's to floaty, try using wheel colliders instead, but keep them invisible. In this latter case, the problem will be making the wheels "soft" enough.

    Also, don't forget to look at mass and drag of the object's rigidbody.
     
  9. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    did you watch


    it is totaly stable
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Oh. I read forces. And hover car.

    For wheels, simply use a wheel collider. Look at the sample assets on the asset store and look at the car example.
     
  11. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    whats point of using wheels coliders i will not learn anything i want to solve this problem
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Then I'm totally confused...

    If you want a vehicle to stick to the ground without bouncing, use a wheel collider. That's what they are designed for.

    If you want a hover car that is suspended above the ground, use physics forces.

    If you don't want to use either, use a frictionless material and just push the object around...

    Your original post was:

    What do you want to do??
     
  13. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    like i sead i am trying to make suspenison like on video from 2 to 3 minut, i dont want to use weele coliders
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, I don't really know what more to say...

    That video tells you everything you need to know about how to make those vehicles work.

    It explains, in detail, how to set up your raycasts and forces, and how the physics is supposed to work for all of the major behaviours.

    If you have not looked at the "learn" site, then you should:
    http://unity3d.com/learn

    And look specifically at the items discussed in the video:
    http://unity3d.com/learn/tutorials/modules/beginner/physics

    ^That section includes Rigidbodies (which allow physics on a GameObject), AddForce to move forward and up, AddTorque to turn, Raycasting to check the ground, and then Colliders and OnCollisionEnter for when you bump your vehicle into something.

    You may also want to check the manual:
    http://docs.unity3d.com/Manual/

    ... and look up each of these items (Rigidbodies, Colliders, etc.) ...

    ... and the Scripting Reference:
    http://docs.unity3d.com/ScriptReference/index.html

    ... for the code required (Physics.Raycast, OnCollisionEnter, etc.)...

    ... and when you've done this, you'll have a better understanding of what's going on in the video.
     
  15. isometricLove

    isometricLove

    Joined:
    Sep 23, 2014
    Posts:
    10
    i done thet all watched all suff and coded and showed you my code but i dont know why it dosent work normal what am i missing
     
  16. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you are willing to optimize your code based on my comments above, then I will have a look at it. Normally I try to point people in the correct direction, rather than write code for them, but if you are completely stuck, I'll try to unstick you, but if I need to run through code, having it compact will make it much easier.
     
  17. FuzzyBalls

    FuzzyBalls

    Joined:
    Jun 12, 2014
    Posts:
    47
    Take a look at mine, I'm having the same issue, it just bounces like a rabbit,

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class SuspensionPoint
    6. {
    7.     public Transform t;
    8.     public float CompressionRatio = 0.0f; // 0.0f - 1.0f
    9.     public Vector3 SurfaceImpactPoint, SurfaceImpactNormal;
    10.     private float RaycastDistance, suspensionForce;
    11.     private int id;
    12.  
    13.     public SuspensionPoint (Transform _t, float rd, float sf, int _id)
    14.     {
    15.         this.id = _id;
    16.         this.t = _t;
    17.         this.RaycastDistance = rd;
    18.         this.suspensionForce = sf;
    19.     }
    20.  
    21.     public void UpdateMe (Transform ct, Rigidbody rb)
    22.     {
    23.         RaycastHit hit;
    24.         if (Physics.Raycast (t.position, -ct.up, out hit, RaycastDistance))
    25.         {
    26.             this.CompressionRatio = hit.distance / RaycastDistance;
    27.  
    28.             rb.AddForceAtPosition (Vector3.up * this.CompressionRatio * suspensionForce, t.position, ForceMode.Acceleration);
    29.  
    30.             Debug.DrawRay (t.position, -ct.up * RaycastDistance, Color.green);
    31.         }
    32.         else
    33.         {
    34.             Debug.DrawRay (t.position, -ct.up * RaycastDistance, Color.red);
    35.         }
    36.     }
    37.  
    38.     public void Draw ()
    39.     {
    40.         Gizmos.DrawSphere (t.position, 0.2f);
    41.     }
    42.  
    43.     public void DoGUI (Rect rect)
    44.     {
    45.         GUI.Label (rect, "CR (" + id + ")    :   " + this.CompressionRatio.ToString ());
    46.     }
    47. }
    48.  
    49. public class Vehicle : MonoBehaviour
    50. {
    51.     public Transform[] corners;
    52.     public float RaycastDistance = 1.0f;
    53.     public float suspensionForce = 30.0f;
    54.  
    55.     public ForceMode fm;
    56.  
    57.     SuspensionPoint[] suspensionPoints = new SuspensionPoint [4];
    58.  
    59.     void Start ()
    60.     {
    61.         suspensionPoints [0] = new SuspensionPoint (corners [0], RaycastDistance, suspensionForce, 0);
    62.         suspensionPoints [1] = new SuspensionPoint (corners [1], RaycastDistance, suspensionForce, 1);
    63.         suspensionPoints [2] = new SuspensionPoint (corners [2], RaycastDistance, suspensionForce, 2);
    64.         suspensionPoints [3] = new SuspensionPoint (corners [3], RaycastDistance, suspensionForce, 3);
    65.     }
    66.  
    67.     void FixedUpdate ()
    68.     {
    69.         foreach (SuspensionPoint point in this.suspensionPoints)
    70.         {
    71.             point.UpdateMe (transform, rigidbody);
    72.         }
    73.     }
    74.  
    75.     void OnDrawGizmos ()
    76.     {
    77.         if (!Application.isPlaying)
    78.             return;
    79.  
    80.         foreach (SuspensionPoint point in this.suspensionPoints)
    81.         {
    82.             point.Draw ();
    83.         }
    84.     }
    85.  
    86.     void OnGUI ()
    87.     {
    88.         for (int i = 0; i < this.suspensionPoints.Length; i++)
    89.         {
    90.             this.suspensionPoints [i].DoGUI (new Rect (10, 10 + (30 * i), 1000, 100));
    91.         }
    92.     }
    93. }
     
  18. FuzzyBalls

    FuzzyBalls

    Joined:
    Jun 12, 2014
    Posts:
    47
    any update?