- Home /
I have a transform problem and I really don't know why
I have a sphere with a rigidbody and no gravity. It controls the camera (or it's supposed to) but unity sends me this error message :
;
transform.position assign attempt for 'Sphere' is not valid. Input position is { NaN, NaN, NaN }. UnityEngine.Transform:set_position(Vector3)\ ;
my code has worked before, but for some reason, unity refuses to admit that the sphere has a transform.position...
;
Here is my code:
// Start is called before the first frame update
void Start()
{
[Range(0.1f, 25f)]
public float speed;
[Range(0.1f, 1f)]
public float sensitivity;
bool up;
bool down;
bool left;
bool right;
bool forward;
bool backward;
float speedChange;
float sensChange;
}
// Update is called once per frame
void Update()
{
// getting inputs
up = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
down = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow);
left = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow);
right = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow);
forward = Input.GetKey(KeyCode.E) || (Input.GetKey(KeyCode.RightShift) && Input.GetKey(KeyCode.UpArrow));
backward = Input.GetKey(KeyCode.Q) || (Input.GetKey(KeyCode.RightShift) && Input.GetKey(KeyCode.DownArrow));
speedChange = Input.mouseScrollDelta.y / Mathf.Abs(Input.mouseScrollDelta.y); // only {-1 ; 0 ; 1}
if(Input.mouseScrollDelta.y != 0 && Input.GetKey(KeyCode.LeftControl))
{
sensChange = Input.mouseScrollDelta.y / Mathf.Abs(Input.mouseScrollDelta.y);
}
;
// applying inputs /////////////////////////THIS IS WHERE THE ERRORS ARE///////////////
if (up) transform.position += transform.up * speed * Time.deltaTime;
if (down) transform.position += transform.up * speed * -1 * Time.deltaTime;
if (left) transform.position += transform.right * speed * -1 * Time.deltaTime;
if (right) transform.position += transform.right * speed * Time.deltaTime;
///////////////////////////////////////END OF ERRORS///////////////////////////
speed += sensitivity * speedChange;
sensitivity += sensChange * .1f;
}
Excuse the format, I don't really know how to work with this site yet
A bit that would help:
the errors are on lines 36-39
Answer by Namey5 · Jul 04, 2020 at 11:02 AM
At line 29;
speedChange = Input.mouseScrollDelta.y / Mathf.Abs(Input.mouseScrollDelta.y);
You most likely end up dividing by 0, thus resulting in the NaNs when you try to increment speed. You should perform checks to make sure that the scrollDelta cannot be 0;
speedChange = Input.mouseScrollDelta.y / Mathf.Abs ((Input.mouseScrollDelta.y == 0f) ? 1f : Input.mouseScrollDelta.y);
That was the problem. Thanks, I didn't think about dividing by zero.
Answer by cpmullen · Jul 04, 2020 at 05:41 PM
I am having the same problem. I am new to Unity as far as scripting.... Here is my code...
public float upf;
private Rigidbody rig;
void Awake ()
{
rig = GetComponent<Rigidbody>();
}
void Update ()
{
Move();
}
void Move ()
{
float xInput = Input.GetAxis("Horizontal") * upf;
float yInput = Input.GetAxis("Vertical") * upf;
}
I am also getting an "Skipped updating the transform of this Rigidbody because its comkponents are infinite. Could you have applied infinite forces, acceleration or set huge velocity?" error. PLEASE HELP ME!
Your answer
Follow this Question
Related Questions
Transform.Translate causing object to teleport 0 Answers
transform.position not working in OnControllerColliderHit 2 Answers
transform.localscale suddenly stopped working!,transform.localscale not working!!! 1 Answer
transform.position working on Unity editor but not on Android phone? 1 Answer
Area Effect Damage 2 Answers