- Home /
Continual position error
Hi everyone, I've been trying to get this code to work for a while now but i'm a bit stumped as to why I keep getting an error when all the other values appear in the inspector. It is a Seek behavior that i'm rewriting in Unityscript from AS3 code.
Here is the code that I have
#pragma strict
var maxForce : float;
var maxVelocity : float;
var position : Vector3;
var desired : Vector3;
var velocity : Vector3;
var target : Vector3;
var steering : Vector3;
var mass : float;
var totalMass : float;
var Target : Transform;
function Boid (posX : float, posY : float, posZ : float)
{
position = new Vector3 (posX, posY, posZ);
velocity = new Vector3 (0, 0, 0);
target = new Vector3 (0, 0, 0);
desired = new Vector3 (0, 0, 0);
steering = new Vector3 (0, 0, 0);
mass = totalMass;
truncate (velocity, maxVelocity);
var x = velocity.x;
var y = velocity.y;
var z = velocity.z;
}
function seek (target : Vector3)
{
var force : Vector3;
desired = (target - position).normalized * maxVelocity;
force = desired - velocity;
return force;
}
function truncate(vector : Vector3, max : float)
{
var i : float;
i = max / vector.magnitude;
i = i < 1.0 ? i : 1.0;
vector * i;
}
function Update ()
{
target = Target.position;
steering = seek(target);
truncate (steering, maxForce);
steering = steering / mass;
velocity = (velocity + steering).normalized * maxVelocity;
truncate (velocity, maxVelocity);
position = transform.position + velocity;
var x = velocity.x;
var y = velocity.y;
var z = velocity.z;
transform.LookAt(position);
transform.Translate (x * Time.deltaTime, y * Time.deltaTime, z * Time.deltaTime);
}
The problem lies in the Seek function (I think), it returns the steering(force) component as -Infinity or NaN and as such, does not allow the object to move as it sets the Velocity to 0 on all axis, I have backwards written the Wander behaviour and that works fine, this however refuses to.
Here is the error as well that comes up.
"transform.position assign attempt for 'Seek' is not valid. Input position is { NaN, NaN, NaN }. UnityEngine.Transform:Translate(Single, Single, Single) Seek:Update() (at Assets/AI/Seek.js:73)"
I'm utterly confused, I even took the Wander script, copy and pasted that and put in the Seek function and it still refuses to work.
Many Thanks
I don't spot the problem producing your NaNs, but I do spot an issue. Your truncate function is not doing anything. That is, you are passing in two variables by value, so any operations on those variables is not reflected in the originals variables. Nothing is returned, so the whole function is a NoOp.
Try, as a thought, using something other than target in the function.
You have a class variable
var target : Vector3;
The following is fine, and uses the class variable.
target = Target.position;
steering = seek(target);
which you are redefining in your function
function seek (target : Vector3)
So, in the function seek, change target to something else, just for use within that function. The argument variable name does not need to match the variable used in the call!!
function seek (seekTarget : Vector3)
{
var force : Vector3;
desired = (seekTarget - position).normalized * maxVelocity;
force = desired - velocity;
return force;
}
To be honest, in some languages this is actually not an issue. I'm new to JS (US) myself but I'm betting this is an issue here :)
Your answer
Follow this Question
Related Questions
AI Evasion help 1 Answer
What's wrong with my AI script. 3 Answers
Button Script is missing after load 1 Answer
2 Scripting Errors I need help fixing!! 3 Answers
Error with Kimmons dialogue system/ Interface must be public or explicit 1 Answer