- Home /
Moving Player Up and Down in 2D
First off here is the code for side to side:
pragma strict
var health : int = 100;
var speed : int = 400;
function Start () {
}
function Update () {
if(Input.GetAxis("Horizontal")){
transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
}
}
What I want to add is my player being able to move up and down on the Y axis. I have done things like this:
pragma strict
var health : int = 100;
var speed : int = 400;
function Start () {
}
function Update () {
if(Input.GetAxis("Horizontal")){
transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
}
if(Input.GetAxis("Vertical")){
transform.Translate(Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0, 0));
}
}
But then it just makes it so when I press W to go up it just moves left, and when I press S to go down it just makes me go right. Then when I try to go side to side on the X axis using A and D it does nothing. The camera angle is like PacMan, so the camera is directly above the seen looking down on the terrain. I used Javacode for the scripts and my main character was built off of a cube. It is a 2D game.
Duplicate Question : http://answers.unity3d.com/questions/356031/adding-movement-on-the-y-axis.html
Please Delete this Question.
Please be patient if your question/reply doesn't show straight away.
As a new user, your posts and questions are held in a moderator que until it is approved and then it is displayed. When your karma rises, you'll be able to post questions, comments and answers without waiting for someone to approve it =]
Also use the formatting tool for code, it makes it alot easier to read
In future (as stated by darker9999), please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting.
I have formatted this one for you. Please check the code is the same as what you have.
I see where you are confused, so I'll just quickly explain Vector3. This is representing the X_axis, Y_axis and Z_axis, in that order. So Vector3( 1, 2, 5 ) means
x = 1
y = 2
z = 5
When you say up/down it actually depends on which way you have built your 'board'. Is it going up (vertical on the Y_axis) or is it laying flat (horizontal on the Z_axis). Now you can see :
for X-Z : Vector3( input_X, 0, input_Y )
for X-Y : Vector3( input_X, input_Y, 0 )
here is the unity scripting reference : http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Vector3.html
Here are some links I strongly suggest to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
Character controller and terrain : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : http://www.unityjumpstart.com/ProofOfConcept_1/ : click on the videos part1.mp4 part2,3,4 =]
I just found another by our own Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial
By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you'll need. For example, if you want to make some terrain then walk around it with a character : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
Here's another on the same 'site but I havn't checked the difficulty : http://cgcookie.com/unity/2012/02/24/game-planning-lunar-lander-01/
Basically then just search for tutorials, there are many out there, either written or on youtube.
the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials
A big list of tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
Helpful page with information on using Built-In Arrays and Lists (you'll need this later!) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner
http://answers.unity3d.com/questions/148211/list-of-frequently-asked-beginners-questions.html
If you know what kind of game you want to make, edit your question and write there what you are thinking of, then there may be a tutorial out there for that!
But just be aware, you will do lots of typing, deleting, typing, bashing head on desk, being really happy when it works and you understand how and why. I hope this helps, Happy Coding =]
Answer by darker9999 · Nov 29, 2012 at 07:21 AM
it should be:
function Update ()
{
if(Input.GetAxis("Horizontal"))
{
transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
}
if(Input.GetAxis("Vertical"))
{
transform.Translate(Vector3(0, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0));
}
}
This will cause the unit to move twice as fast when moving diagonally, I gave a solution for this in my answer here: http://answers.unity3d.com/questions/356037/moving-on-the-y-axis-javascript.html