Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by MrADunne · Dec 02, 2013 at 05:27 PM · javascripterrorfollow pathtransform.

How to solve error transform.position assign attempt for 'Car' is not valid. Input position is { NaN, NaN, NaN }.

In my game i have a car following a waypoint system, everyting seems to work perfectly but after a random amount of time my AI car stops moving and gives me the error transform.position assign attempt for 'Car' is not valid. Input position is { NaN, NaN, NaN }. This is the code i am using. Any help in solving my problem would be much appreciated.

 var path : Array;
 var pathGroup : Transform;
 var maxSteer : float = 15.0;
 var wheelFL : WheelCollider;
 var wheelFR : WheelCollider;
 var wheelRL : WheelCollider;
 var wheelRR : WheelCollider;
 var currentPathObj : int;
 var direction : float;
 var distancefrompath :float = 5.0;
 var maxTorque : float = 50.0;
 var currentSpeed : float;
 var topSpeed : float = 50.0;
 var decellerationSpeed : float = 10.0;
 var steerVector : Vector3;
 
 function Start()
 {
     rigidbody.centerOfMass.y = -3.5;
     GetPath();
 }
 
 function GetPath()
 {
     var path_objs : Array = pathGroup.GetComponentsInChildren(Transform);
     path = new Array();
     
     for (var path_obj : Transform in path_objs)
     {
         if(path_obj != pathGroup)
         {
             path[path.length] = path_obj;
         }
     }
     Debug.Log(path.length);
 }
 function Update()
 {
     GetSteer();
     Move();
 }
 
 function GetSteer()
 {
      steerVector = transform.InverseTransformPoint(Vector3(path[currentPathObj].position.x,transform.position.y,transform.position.z));
     var newSteer : float = maxSteer * (steerVector.x / steerVector.magnitude);
     direction = steerVector.x / steerVector.magnitude;
     wheelFL.steerAngle = newSteer;
     wheelFR.steerAngle = newSteer;
     if (steerVector.magnitude <= distancefrompath)
     {
         currentPathObj++;
         
         if (currentPathObj >= path.length)
         {
             currentPathObj = 0;
         }
     }
 }
 function Move()
 {
     currentSpeed = 2*(22/7)*wheelRL.radius*wheelRL.rpm* 60/1000;
     currentSpeed = Mathf.Round(currentSpeed);
     if(currentSpeed <= topSpeed)
     {
         wheelRL.motorTorque = maxTorque;
         wheelRR.motorTorque = maxTorque;
         wheelRL.brakeTorque = 0;
         wheelRR.brakeTorque = 0;
     }
     else
     {
         wheelRL.motorTorque = 0;
         wheelRR.motorTorque = 0;
         wheelRL.brakeTorque = decellerationSpeed;
         wheelRR.brakeTorque = decellerationSpeed;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by meat5000 · Dec 02, 2013 at 05:29 PM

Perhaps you are getting divide by zero error in the two lines

 var newSteer : float = maxSteer * (steerVector.x / steerVector.magnitude);
 direction = steerVector.x / steerVector.magnitude;


Do you get the error when your character has reached the current path location? i.e the point at which InverseTransformPoint chucks in a load of zeros?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrADunne · Dec 02, 2013 at 06:12 PM 0
Share

There is no set point at which the error occurs, sometimes the error occurs when i am nearly at the finish point and other times the car might have only moving a number of seconds.

avatar image meat5000 ♦ · Dec 02, 2013 at 06:32 PM 0
Share

Path array argument updates each time you cross the threshold distance, correct? You check for

As a test, try updating this line

 steerVector = transform.InverseTransformPoint(Vector3(path[currentPathObj].position.x,transform.position.y+0.001,transform.position.z+0.001))

In local space an object at the point of your Player will have 0,0,0 local transform. This will make magnitude zero and produce NaN. Adding in a very tiny value to y and z may prevent this, so add the numbers in and see if the error still occurs.

avatar image MrADunne · Dec 02, 2013 at 06:45 PM 0
Share

fixed it perfectly, i had a feeling from the beggining that it was dividing by zero, i just couldnt see the solution because i have been looking at it for so long.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sound system not working correctly 0 Answers

Collectible/Counter Script Error 1 Answer

[Java Script] Why is my script not working? 1 Answer

BCE0005: Unknown identifier: 'spriteRenderer'. 2 Answers

Unity freezes when I add a special script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges