Search Unity

How does an object's position become invalid?

Discussion in 'Scripting' started by glom1envisage0, Jan 5, 2012.

  1. glom1envisage0

    glom1envisage0

    Joined:
    Apr 3, 2011
    Posts:
    167
    I have an error 'transform.position assign attempt for 'CharacterController' is not valid. Input position is {NaN, NaN, NaN}. When this happens the character gets 'stuck' and no long responds to input. What script mistakes in general might lead to an invalid position?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You would need to provide some script. It would be just guessing at the answer without it.
     
  3. Sornelo

    Sornelo

    Joined:
    Dec 31, 2011
    Posts:
    33
    Is there a certain spot where you get stuck? Try adjusting the character controller properties a bit.
     
  4. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
  5. glom1envisage0

    glom1envisage0

    Joined:
    Apr 3, 2011
    Posts:
    167
    As a possible solution, at the end of the Update function in my control script I put:
    if(transform.position.x == Mathf.Infinity)
    {
    transform.position.x = 999999;
    }
    if(transform.position.y == Mathf.Infinity)
    {
    transform.position.y = 999999;
    }
    if(transform.position.z == Mathf.Infinity)
    {
    transform.position.z = 999999;
    }
    if(moveDirection.x == Mathf.Infinity)
    {
    moveDirection.x = 999999;
    }
    if(moveDirection.y == Mathf.Infinity)
    {
    moveDirection.y = 999999;
    }
    if(moveDirection.z == Mathf.Infinity)
    {
    moveDirection.z = 999999;
    }

    So far, I hadn't ran into the error again yet, but I'm not sure if it's just by chance or if I created an exception handler that works.
     
  6. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    That would solve the issue, but there are better ways. If you show your movement code maybe we could help you avoid that, as it's a workaround more than a real solution, and then you won't run into it again.

    Edit: Also, if it's 0/0 that's causing it, rather than just rapid movement, you could find yourself teleporting about.
     
  7. YosemiteSam

    YosemiteSam

    Joined:
    May 5, 2011
    Posts:
    60
    Infinity isn't the same as NaN. NaN is not equal to any value, even NaN. I think Ntero is right, somewhere a 0/0 or square root of a negative number has happened and been applied to a variable in your transform. Maybe you used Quaternion.AngleAxis with a zero length vector. That's just a guess but maybe something like that.
     
  8. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Yeah My mistake NaN, does not include +/- Infinity, so that solution won't actually work at all. If you show some code it should hopefully be quick to spot the culprit.
     
  9. glom1envisage0

    glom1envisage0

    Joined:
    Apr 3, 2011
    Posts:
    167
    I think the problem is here:
    var right = Vector3(forward.z*MainCameraScript.CamDist(), 0, forward.x*MainCameraScript.CamDist());

    CamDist() is a function in a script I'm referencing that is:
    function CamDist()
    {
    var DistRev : float;
    DisRev = (50 - currentDistance)/10;
    return DisRev;
    }

    I used the function set the 'turning' to be relative to how far the camera is
    from the player. It's for when the character is in a vehicle. I have the max distance to be
    no greather than 40, but there may be moments when it will be higher for a split second.