Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

'transform.position assign attempt for ... is not valid' and 'Invalid AABB' errors

Discussion in 'Physics' started by GWiersema, May 17, 2017.

  1. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    Hi, I am new to unity and tried a tutorial series form 'Brackeys' on youtube. I had a bug and his completed project has the same bug. Here you can download the project for free: http://devassets.com/assets/how-to-make-a-video-game/ .

    It happens when my player collides with the ground at a certain angle. It can't set the transform.position of main camera and the game freezes/crashes. In the console there will be 999+ errors but the first one will be:

    transform.position assign attempt for 'Main Camera' is not valid. Input position is { NaN, NaN, NaN }.
    UnityEngine.Transform:set_position(Vector3)
    FollowPlayer:Update() (at Assets/Scripts/FollowPlayer.cs:11)

    Here is a video where I show the bug:


    I read some things about dividing by 0 but i don't see anywhere in the script where that would happen. Also I read that it might have something tot do with a particle system, but i don't have a particle system in this project. I tried several things already but nothing worked. If anyone knows how to fix this, please tell me...
     
    Last edited: May 17, 2017
  2. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    Can you post the FollowPlayer script.
    Is the player being removed or reset in some way just before the error?
     
    dieqoo likes this.
  3. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    Here is my FollowPlayer script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FollowPlayer : MonoBehaviour {
    4.  
    5.     public Transform player;    // A variable that stores a reference to our Player
    6.     public Vector3 offset;        // A variable that allows us to offset the position (x, y, z)
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.         // Set our position to the players position and offset it
    11.         transform.position = player.position + offset;
    12.     }
    13. }
    14.  
    And here is my PlayerMovement:

    Code (CSharp):
    1.     public Rigidbody rb;
    2.  
    3.     public float forwardForce = 2000f;    // Variable that determines the forward force
    4.     public float sidewaysForce = 500f;  // Variable that determines the sideways force
    5.  
    6.     // We marked this as "Fixed"Update because we
    7.     // are using it to mess with physics.
    8.     void FixedUpdate ()
    9.     {
    10.         // Add a forward force
    11.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    12.  
    13.         if (Input.GetKey("d"))    // If the player is pressing the "d" key
    14.         {
    15.             // Add a force to the right
    16.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    17.         }
    18.  
    19.         if (Input.GetKey("a"))  // If the player is pressing the "a" key
    20.         {
    21.             // Add a force to the left
    22.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    23.         }
    24.  
    25.         if (rb.position.y < -1f)
    26.         {
    27.             FindObjectOfType<GameManager>().EndGame();
    28.         }
    29.     }
    30. }
    31.  
    And my GameManager:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     bool gameHasEnded = false;
    7.  
    8.     public float restartDelay = 1f;
    9.  
    10.     public GameObject completeLevelUI;
    11.  
    12.     public void CompleteLevel ()
    13.     {
    14.         completeLevelUI.SetActive(true);
    15.     }
    16.  
    17.     public void EndGame ()
    18.     {
    19.         if (gameHasEnded == false)
    20.         {
    21.             gameHasEnded = true;
    22.             Debug.Log("GAME OVER");
    23.             Invoke("Restart", restartDelay);
    24.         }
    25.     }
    26.  
    27.     void Restart ()
    28.     {
    29.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    30.     }
    31.  
    32. }
    33.  
    And here is my PlayerCollision:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerCollision : MonoBehaviour {
    4.  
    5.     public PlayerMovement movement;     // A reference to our PlayerMovement script
    6.  
    7.     // This function runs when we hit another object.
    8.     // We get information about the collision and call it "collisionInfo".
    9.     void OnCollisionEnter (Collision collisionInfo)
    10.     {
    11.         // We check if the object we collided with has a tag called "Obstacle".
    12.         if (collisionInfo.collider.tag == "Obstacle")
    13.         {
    14.             movement.enabled = false;   // Disable the players movement.
    15.             FindObjectOfType<GameManager>().EndGame();
    16.         }
    17.     }
    18.  
    19. }
    20.  
    All the code is the same as in the project in the link. Other scripts are EndTrigger, LevelComplete and Score. But i don't think those are relevant.

    I really appreciate any help you can provide.
     
    Last edited: May 17, 2017
  4. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    is the error when the scene is about to changed? comment out the line below and see if still stops at the same point.
    Code (csharp):
    1.  
    2.       // SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    3.  
    If that does not work try commenting out
    Code (csharp):
    1.  
    2. if (rb.position.y< -1f)
    3.         {
    4.             // FindObjectOfType<GameManager>().EndGame();
    5.         }
    6. }
    7.  
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,844
    What version of Unity since these errors shouldn't really be happening unless you're dividing by zero or something.
     
  6. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    I'm using unity 5.6.0f3. It's also visible in the top of the youtube video. So the newest version if I'm correct...
     
  7. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    I tried both, but they didn't work. I got the same error both times. Thanks for the try tho.
     
  8. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    odd.

    try (replace 1,2,3 with your values from the inspector window)
    Code (csharp):
    1.  
    2.   transform.position = player.position + new Vector3(1, 2, 3);
    3.  
     
  9. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    I did what you said and replaced the line with:
    Code (CSharp):
    1. transform.position = player.position + new Vector 3(0f, 1.2f, -5.5f);
    and deleted the
    Code (CSharp):
    1. public Vector3 offset;
    Still got the same error, thanks again for trying. Maybe it's a bug in Unity or something?

    P.s: If you want to try something out yourself you can download the project from the link in my first message.
     
    Last edited: May 17, 2017
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,844
    You're best off positing the project as a bug report I think.

    Help->Report a Bug
     
  11. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    Takes a few goes but have recreated the same error on 5.5.1f1.
    Will have a little play
     
  12. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    Okay, I will post a bug report. Thanks for trying to help. As a type of the Issue i clicked A problem with the editor. I hope thats's the correct one.
     
  13. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    If you find a solution, please tell me. Really appreciate the fact that you're trying to help. I'm amazed how quick and easy it is for a beginner like me to try to get some help. :)
     
  14. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    The error occurs when the player collides with the side of the ground gameobject.
    It looks like the physics engine is then updating the player position to NaN,NaN,NaN
    I think it’s related the large scale of the ground gameobject.

    Try setting the ground gameobject Z scale to 1000 and move its z position to 500.
     
  15. GWiersema

    GWiersema

    Joined:
    Apr 19, 2017
    Posts:
    9
    Wow, that actually works. Seems like unity can't do the calculations with objects larger than a certain scale. I changed it and the bug is now gone. Really appreciated the help, thank you very very much. Finally I can finish the game :)
     
    Donay likes this.
  16. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    You’re welcome. It’s probably more of a physics engine issue that a limitation within unity. Due to the large scale I suspect a floating point value may be getting rounded off to zero, causing the NaN in the objects position.
     
    GWiersema likes this.
  17. dieqoo

    dieqoo

    Joined:
    Mar 22, 2017
    Posts:
    3
    disnable the project settings->physics->Enable PCM is work too.

    i think this is unity physics PCM bug , my game have same problem too. you should report this
     
  18. dariotito18

    dariotito18

    Joined:
    Feb 19, 2018
    Posts:
    1
    diegoo that is pcm, i cant see it upload_2018-3-9_1-23-13.png
     
  19. austephner

    austephner

    Joined:
    Jul 20, 2016
    Posts:
    2
    getting this exception thrown even when put the code causing it into a try/catch block (meaning the try/catch fails to handle the exception). I thought it was just that Unity didn't compile correctly but that theory wasn't correct.

    Line 19 causes the exception when the _mechs List suddenly contains a null object (disconnecting from server destroys networked objects). Excuse the messy code I've just been bending over backwards trying to figure out why this is happening.

    EDIT
    This script is part of a behaviour on the MainCamera.

    Code (CSharp):
    1.     void LateUpdate ()
    2.     {
    3.         _averagePosition = transform.position;
    4.  
    5.         if (_mechs.Count == 0) _mechs = new List<MechBehaviour>(MechManager.singleton.LocalMechs);
    6.         if (_mechs.Count == 0) return;
    7.  
    8.         try
    9.         {
    10.             _averagePosition = Vector3.zero;
    11.             var nonNullMechs = _mechs.Where(m => m != null).ToArray();
    12.  
    13.             foreach (MechBehaviour m in nonNullMechs)
    14.                 _averagePosition += m.transform.position;
    15.  
    16.             _averagePosition /= nonNullMechs.Length;
    17.             _averagePosition.y = transform.position.y;
    18.  
    19.             transform.position = Vector3.Lerp(transform.position, _averagePosition, Time.deltaTime * CameraSpeed);
    20.         }
    21.         catch
    22.         {
    23.             _mechs = new List<MechBehaviour>();
    24.             _averagePosition = transform.position;
    25.         }
    26.  
    27.     }
     
    Last edited: May 5, 2018
  20. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    45
    I'm getting this consistently with DeepMotion's engine, and they've confirmed I'm doing everything correctly. Definitely an issue with Unity Physics, it seems.
     
  21. piwxel175

    piwxel175

    Joined:
    Nov 15, 2018
    Posts:
    2
    lI have the solution very easy will do the scene it is necessary to remove the window of scene and put back and that is at 100%
     
    RushSecond likes this.
  22. JPhilipp

    JPhilipp

    Joined:
    Oct 7, 2014
    Posts:
    22
    SavedByZero I'm having the same issue when using ML-Agents (machine learning with Python Google TensorFlow), did you ever get it figured out?