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…
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.
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.
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
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.
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.