- Home /
Vector3 Returning Infinity Error
Hi all,
I'm following a tutorial on YouTube so I've seen this guys code working, however when I try to replicate it just won't work at all. I'm getting an error saying
"transform.position assign attempt for 'Player' is not valid. Input position is {- Infinity, - Infinity , Infinity}.
But on his it seems to work just fine so i'm rather confused. This is the first time I've ever used Unity so perhaps I've over looked something? Code is as follows.
Any help would be really appreciate! Thank You
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private Transform myTransform;
public int playerSpeed = 5;
// Use this for initialization
void Start () {
myTransform = transform;
//here is the spawn point for the player (roughly centre of game window) (-3,-3,-1)
myTransform.position = new Vector3 (-3, -3, -1);
}
// Update is called once per frame
void Update () {
//add some player controls (movement etc)
myTransform.Translate (Vector3.right * playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
}
}
It works fine for me, perhaps your input manager has something bizarre for Input.GetAxis("Horizontal") ?
Answer by wibble82 · Mar 07, 2014 at 05:20 PM
I suspect Input.GetAxis is returning dodgy numbers (maybe you have no controller plugged in, or the controller isn't working?).
Above where you set the transform try adding: Debug.Log(playerSpeed); Debug.Log(Input.GetAxis("Horizontal"));
I suspect one of the 2 values printed out will be dodgy!
I don't have a controller at this point, but will assign to a keyboard control i'm assu$$anonymous$$g. In the tutorial he writes the code as I have it, returns to the game scene and presses play. The 'cube' object (which is the player atm) moves to the start position and then tracks right depending on the player speed.
Also, where do you mean I should add the code you suggested?
Above line 25. Then it'll print out the 2 values to the console every frame. I suspect that since you don't have a controller plugged in (which is what Input.GetAxis reads from) its just giving you dodgy data.
Answer by Eddie8787 · Mar 07, 2014 at 05:38 PM
No idea at all how...but now its working. I'm not even sure I have made a code change to make it work. It just does. Thanks for the help anyway! That debug log tip will be useful in the future i'm sure. Cheers