I assume this is the code you're asking about:
// If (velocity + steering) equals zero, then there is no movement
velocity = truncate(velocity + steering, max_speed)
position = position + velocity
function truncate(vector:Vector3D, max:Number) :void {
var i :Number;
i = max / vector.length;
i = i < 1.0 ? i : 1.0;
vector.scaleBy(i);
}
First, the truncate function creates a variable i
with type Number
. Then, it assigns to i
the maximum speed divided by the length of the velocity plus steering vectors. The next line is a ternary operator, but I don't think those exist in GDScript. If i
is less than 1.0, then assign that to i
. Else, assign 1.0 to i
. Then the vector is scaled by i
.
So to replicate this in GDScript I would do something like:
func truncate(vector, max):
var i = max / vector.length()
i = min(i, 1.0)
return vector * i
I'm not 100% sure but I think that Vector3
s are passed as values and not references so you'll have to return the vector instead of void
like the AS3 function does.