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

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: How To Make A Video Game | Dev Assets .

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…

Can you post the FollowPlayer script.
Is the player being removed or reset in some way just before the error?

1 Like

Here is my FollowPlayer script:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform player;    // A variable that stores a reference to our Player
    public Vector3 offset;        // A variable that allows us to offset the position (x, y, z)
 
    // Update is called once per frame
    void Update () {
        // Set our position to the players position and offset it
        transform.position = player.position + offset;
    }
}

And here is my PlayerMovement:

    public Rigidbody rb;

    public float forwardForce = 2000f;    // Variable that determines the forward force
    public float sidewaysForce = 500f;  // Variable that determines the sideways force

    // We marked this as "Fixed"Update because we
    // are using it to mess with physics.
    void FixedUpdate ()
    {
        // Add a forward force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (Input.GetKey("d"))    // If the player is pressing the "d" key
        {
            // Add a force to the right
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if (Input.GetKey("a"))  // If the player is pressing the "a" key
        {
            // Add a force to the left
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if (rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();
        }
    }
}

And my GameManager:

using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {

    bool gameHasEnded = false;

    public float restartDelay = 1f;

    public GameObject completeLevelUI;

    public void CompleteLevel ()
    {
        completeLevelUI.SetActive(true);
    }

    public void EndGame ()
    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log("GAME OVER");
            Invoke("Restart", restartDelay);
        }
    }

    void Restart ()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

}

And here is my PlayerCollision:

using UnityEngine;

public class PlayerCollision : MonoBehaviour {

    public PlayerMovement movement;     // A reference to our PlayerMovement script

    // This function runs when we hit another object.
    // We get information about the collision and call it "collisionInfo".
    void OnCollisionEnter (Collision collisionInfo)
    {
        // We check if the object we collided with has a tag called "Obstacle".
        if (collisionInfo.collider.tag == "Obstacle")
        {
            movement.enabled = false;   // Disable the players movement.
            FindObjectOfType<GameManager>().EndGame();
        }
    }

}

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.

is the error when the scene is about to changed? comment out the line below and see if still stops at the same point.

      // SceneManager.LoadScene(SceneManager.GetActiveScene().name);

If that does not work try commenting out

if (rb.position.y< -1f)
        {
            // FindObjectOfType<GameManager>().EndGame();
        }
}

What version of Unity since these errors shouldn’t really be happening unless you’re dividing by zero or something.

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…

I tried both, but they didn’t work. I got the same error both times. Thanks for the try tho.

odd.

try (replace 1,2,3 with your values from the inspector window)

  transform.position = player.position + new Vector3(1, 2, 3);

I did what you said and replaced the line with:

transform.position = player.position + new Vector 3(0f, 1.2f, -5.5f);

and deleted the

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.

You’re best off positing the project as a bug report I think.

Help->Report a Bug

Takes a few goes but have recreated the same error on 5.5.1f1.
Will have a little play

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.

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. :slight_smile:

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.

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

1 Like

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.

1 Like

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

diegoo that is pcm, i cant see it 3418294--269605--upload_2018-3-9_1-23-13.png

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.

    void LateUpdate ()
    {
        _averagePosition = transform.position;

        if (_mechs.Count == 0) _mechs = new List<MechBehaviour>(MechManager.singleton.LocalMechs);
        if (_mechs.Count == 0) return;

        try
        {
            _averagePosition = Vector3.zero;
            var nonNullMechs = _mechs.Where(m => m != null).ToArray();

            foreach (MechBehaviour m in nonNullMechs)
                _averagePosition += m.transform.position;

            _averagePosition /= nonNullMechs.Length;
            _averagePosition.y = transform.position.y;

            transform.position = Vector3.Lerp(transform.position, _averagePosition, Time.deltaTime * CameraSpeed);
        }
        catch
        {
            _mechs = new List<MechBehaviour>();
            _averagePosition = transform.position;
        }

    }

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.