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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Physics Error

Discussion in 'Scripting' started by Tyler Ridings, Feb 14, 2011.

  1. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    You guys mind helping me get this physics error solved(gmc is the name of the game object)

    rigidbody.force assign attempt for 'Gmc' is not valid. Input position is { NaN, NaN, NaN }.
    UnityEngine.Rigidbody:AddForceAtPosition(Vector3, Vector3)
    Wheel:FixedUpdate() (at Assets/Scripts/Wheel.js:86)

    Code (csharp):
    1. var mass = 1.00;
    2. var wheelRadius = 0.00;
    3. var suspensionRange = 0.00;
    4. var suspensionForce = 0.00;
    5. var suspensionDamp = 0.00;
    6. var compressionFrictionFactor = 0.00;
    7.  
    8. var sidewaysFriction = 0.00;
    9. var sidewaysDamp = 0.00;
    10. var sidewaysSlipVelocity = 0.00;
    11. var sidewaysSlipForce = 0.00;
    12. var sidewaysSlipFriction = 0.00;
    13. var sidewaysStiffnessFactor = 0.00;
    14.  
    15. var forwardFriction = 0.00;
    16. var forwardSlipVelocity = 0.00;
    17. var forwardSlipForce = 0.00;
    18. var forwardSlipFriction = 0.00;
    19. var forwardStiffnessFactor = 0.00;
    20.  
    21. var frictionSmoothing = 1.00;
    22.  
    23. private var hit : RaycastHit;
    24.  
    25. private var parent : Rigidbody;
    26. parent = transform.root.rigidbody;
    27.  
    28. private var graphic : Transform;
    29. graphic = transform.FindChild("Graphic");
    30.  
    31. private var wheelCircumference = 0.00;
    32. wheelCircumference = wheelRadius * Mathf.PI * 2;
    33.  
    34. private var usedSideFriction = 0.00;
    35. private var usedForwardFriction = 0.00;
    36. private var sideSlip = 0.00;
    37. private var forwardSlip = 0.00;
    38.  
    39. var car : Car;
    40. var driven = false;
    41.  
    42. var speed = 0.00;
    43.  
    44. var brake = false;
    45. var skidbrake = false;
    46.  
    47. function FixedUpdate ()
    48. {
    49.     down = transform.TransformDirection(Vector3.down);
    50.     if(Physics.Raycast (transform.position, down, hit, suspensionRange + wheelRadius)  hit.collider.transform.root != transform.root)
    51.     {
    52.         velocityAtTouch = parent.GetPointVelocity(hit.point);
    53.        
    54.         compression = hit.distance / (suspensionRange + wheelRadius);
    55.         compression = -compression + 1;
    56.         force = -down * compression * suspensionForce;
    57.        
    58.         t = transform.InverseTransformDirection(velocityAtTouch);
    59.         t.z = t.x = 0;
    60.         shockDrag = transform.TransformDirection(t) * -suspensionDamp;
    61.        
    62.         forwardDifference = transform.InverseTransformDirection(velocityAtTouch).z - speed;
    63.         newForwardFriction = Mathf.Lerp(forwardFriction, forwardSlipFriction, forwardSlip * forwardSlip);
    64.         newForwardFriction = Mathf.Lerp(newForwardFriction, newForwardFriction * compression * 1, compressionFrictionFactor);
    65.         if(frictionSmoothing > 0)
    66.             usedForwardFriction = Mathf.Lerp(usedForwardFriction, newForwardFriction, Time.fixedDeltaTime / frictionSmoothing);
    67.         else
    68.             usedForwardFriction = newForwardFriction;
    69.         forwardForce = transform.TransformDirection(Vector3(0, 0, -forwardDifference)) * usedForwardFriction;
    70.         forwardSlip = Mathf.Lerp(forwardForce.magnitude / forwardSlipForce, forwardDifference / forwardSlipVelocity, forwardStiffnessFactor);
    71.        
    72.         sidewaysDifference = transform.InverseTransformDirection(velocityAtTouch).x;
    73.         newSideFriction = Mathf.Lerp(sidewaysFriction, sidewaysSlipFriction, sideSlip * sideSlip);
    74.         newSideFriction = Mathf.Lerp(newSideFriction, newSideFriction * compression * 1, compressionFrictionFactor);
    75.         if(frictionSmoothing > 0)
    76.             usedSideFriction = Mathf.Lerp(usedSideFriction, newSideFriction, Time.fixedDeltaTime / frictionSmoothing);
    77.         else
    78.             usedSideFriction = newSideFriction;
    79.         sideForce = transform.TransformDirection(Vector3(-sidewaysDifference, 0, 0)) * usedSideFriction;
    80.         sideSlip = Mathf.Lerp(sideForce.magnitude / sidewaysSlipForce, sidewaysDifference / sidewaysSlipVelocity, sidewaysStiffnessFactor);
    81.        
    82.         t = transform.InverseTransformDirection(velocityAtTouch);
    83.         t.z = t.y = 0;
    84.         sideDrag = transform.TransformDirection(t) * -sidewaysDamp;
    85.        
    86.         parent.AddForceAtPosition(force + shockDrag + forwardForce + sideForce + sideDrag, hit.point);
    87.  
    88.         if(driven)
    89.             car.AddForceOnMotor (forwardDifference * usedForwardFriction * Time.fixedDeltaTime);
    90.         else
    91.             speed += forwardDifference;
    92.  
    93.         graphic.position = transform.position + (down * (hit.distance - wheelRadius));
    94.     }
    95.     else
    96.     {
    97.         graphic.position = transform.position + (down * suspensionRange);
    98.     }
    99.    
    100. stopMeBeforeIKillAgain = 360 * (speed / wheelCircumference) * Time.fixedDeltaTime;
    101.     if(stopMeBeforeIKillAgain==stopMeBeforeIKillAgain) graphic.transform.Rotate(stopMeBeforeIKillAgain, 0, 0);
    102. }
    103.  
    104.  
     
  2. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    Comments and Debug are useful.

    I guess it's -
    parent.AddForceAtPosition(force + shockDrag + forwardForce + sideForce + sideDrag, hit.point)

    The function AddForceAtPosition take two Vector3s.
     
  3. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    What do you mean by debug?Oh by the way i got it fixed thanks!
     
  4. handsomewt

    handsomewt

    Joined:
    Apr 5, 2011
    Posts:
    1
    The fault is speed equals float.NaN.
     
  5. Udit-Makwana

    Udit-Makwana

    Joined:
    Jun 1, 2013
    Posts:
    4
    I got this error in this Please help me out .
    Thanks in advance


    rigidbody.velocity assign attempt for 'stone_1(Clone)' is not valid. Input velocity is { NaN, NaN, NaN }.
    UnityEngine.Rigidbody:set_velocity(Vector3)
    enemytouch:Shoot1() (at Assets/Scripts/enemytouch.js:100)