- Home /
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;
}
}
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?
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.
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.
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
Follow this Question
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