- Home /
X axis, y u no work?
Vertical Y axis works, but even on a separate js file I can't make this work. I just started to use js and I'm not familiar with it. Help? #pragma strict
var moveLeft : KeyCode;
var moveRight : KeyCode;
var speed : float = 20;
function Update ()
{
if (Input.GetKey(moveLeft))
{
rigidbody2D.velocity.x = speed;
}
else if (Input.GetKey(moveRight))
{
rigidbody2D.velocity.x = speed *-1;
}
else
{
rigidbody2D.velocity.x = 0;
}
}
Answer by AndreyL · Jun 11, 2014 at 06:14 PM
Are you initializing the 'moveLeft' and 'moveRight' vars somewhere?
for example: var moveLeft = KeyCode.LeftArrow
You can also write instead:
rigidbody2D.velocity.x = speed * Input.GetAxis ("Horizontal");
GetAxis returns a number from -1 to 1 based on the input. It can save you some precious lines of code.
Tried everything, character still doesn't move horizontally. $$anonymous$$eys are bindable in the unity itself. Where the scripts are added to objects.
#pragma strict
var moveUp : $$anonymous$$eyCode;
var moveDown : $$anonymous$$eyCode;
var speed : float = 20;
function Update ()
{
if (Input.Get$$anonymous$$ey(moveUp))
{
rigidbody2D.velocity.y = speed;
}
else if (Input.Get$$anonymous$$ey(moveDown))
{
rigidbody2D.velocity.y = speed *-1;
}
else
{
rigidbody2D.velocity.y = 0;
}
}
This is vertical movements, which works.
Answer by snemishfish · Sep 03, 2014 at 09:46 AM
I'm stuck with the same problem with exactly the same code, the player moves on the y axis but doesn't move on the x.
Have you managed to get your player moving on the x axis?
Your answer
Follow this Question
Related Questions
auto run key 1 Answer
FPShooter sideway/forward dash? 1 Answer
Why doesn't this motion script work? 2 Answers
Make Lerp or other more fluid or continuous 3 Answers
Networking Synchronize Problem. 0 Answers