- Home /
Moving on the Y Axis (Javascript)
I have a 2D game in which the camera is angled like PacMan. The X axis movement, Left and Right, work good but I can figure out how to do up and down movement. Here is my coding:
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));
}
}
Help me figure out what to type for Up and Down. I am using Javascript.
I took the time to format your question correctly.
duplicate question found here : http://answers.unity3d.com/questions/356047/moving-player-up-and-down-in-2d.html
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
Answer by The-Oddler · Nov 29, 2012 at 12:05 PM
I Think this should work:
function Update () {
var hor = Input.GetAxis("Horizontal");
var ver = Input.GetAxis("Vertical");
transform.Translate(Vector3(hor * speed * Time.deltaTime, ver * speed * Time.deltaTime, 0)); // Might want to swap the Y and Z here, depends on how your camera is rotated.
}
Note: If you use this, you'll move twice as fast if you're moving diagonally. If you don't want this, try this:
Vector3(hor, ver).normalized * speed * Time.deltaTime
Note: If I'm not mistaking, GetAxis("Horizontal") doesn't return a value between 0 an 1 (depends on your input settings what it returns.) If you normalize you'll loose this, so your speed will probably have to be higher to make it move properly.
Hope it helps ;)
Answer by DeveshPandey · Nov 29, 2012 at 12:11 PM
Try this :
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(0,Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0));
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Alternative to transform.Translate? 3 Answers
click to move workig script!! but pls help with rotation!! :( 1 Answer
All Unity Engine games' controls not working 1 Answer
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers