Ok so I'm VERY new to Unity and I'm working on my first practice game but the only thing in my way right now is that I'm having trouble making a game object move horizontally and vertically
I have a script for each one and the horizontal one works but the vertical one just makes it go even more horizontal
//horizontal
function Update () {
var horiz : float = Input.GetAxis("Horizontal");
transform.Translate(Vector3(horiz,0,0));
}
And for the vertical I have
//vertical
function Update () {
var vert : float = Input.GetAxis("Vertical");
transform.Translate(Vector3(vert,0,0));
}
I know it's a noob question but I'm really stuck on it
I can tell you are extremely new to this and don't really understand what the functions you are using actually 'do'. I advice to you make good use of this website to look anything you use up.
Let me explain the basic idea here though.
The Input.GetAxis horizontal and vertical stuff is a predefined function of unity, saving you the trouble of having to do this yourself. What it does is when a positive horizontal key is pressed (right arrow or d if you use wasd) it gives a positive value and viceversa. For vertical it does the same. So this variable you defined is either positive or negative depending on the key imput.
Now put this into a Vector3(x,y,z): Vector3(horiz,0,vert) will now 'point' into the direction of the arrow key are pressing. Since you want it to move into this direction take this as the direction of your movement and multiply it by your desired speed over the desired time. Time is important here, else the speed will depend on how quick your computer is.
You're using the same Vector3 for both horizontal and vertical. You'd need to use a different axis for vertical. Also, that code is framerate-dependent, and will run at different speeds on different devices (as well as on the same device, depending on what else is going on). You need to use Time.deltaTime.
Here is the solution, you have to use both together, but remember to use a different axis like other people said for vertical and horizontal inputs.
//horizontal and vertical
function Update () {
var vert : float = Input.GetAxis("Vertical");
var horiz : float = Input.GetAxis("Horizontal");
transform.Translate(Vector3(horiz,0,vert));
}