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
0
Question by Supershandy · Oct 27, 2013 at 02:26 PM · javascripterroraibehaviornan

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

Comment
Add comment · Show 2
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 robertbu · Oct 27, 2013 at 03:11 PM 0
Share

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.

avatar image meat5000 ♦ · Oct 27, 2013 at 04:23 PM 1
Share

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 :)

0 Replies

· Add your reply
  • Sort: 

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

16 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

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


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