- Home /
Question by
ElfElix · Aug 22, 2014 at 03:43 AM ·
2dspritecontroller
Sprite moves infinitely on x axis
The Y works perfectly well, but the X (left and/or right) just makes the sprit travel on infinitely.
Question: What's wrong with it? and how do I fix it?
#pragma strict
var moveUp : KeyCode;
var moveDown : KeyCode;
var moveRight : KeyCode;
var moveLeft : KeyCode;
var speed : float = 5;
function Update () {
if (Input.GetKeyDown(moveLeft)){
rigidbody2D.velocity.x = speed;
} else if (Input.GetKeyDown(moveRight)) {
rigidbody2D.velocity.x = speed * -1;
} else if (Input.GetKeyDown(moveUp)) {
rigidbody2D.velocity.y = speed;
} else if (Input.GetKeyDown(moveDown)) {
rigidbody2D.velocity.y = speed * -1;
} else {
rigidbody2D.velocity.y = 0;
rigidbody2D.velocity.x = 0;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613161224im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
sprite character rotation 0 Answers
Character coltroller direction 0 Answers
Adding a Sprite 1 Answer
Sprite with non-constant jitter during constant velocity movement. 2 Answers
How to alternate images in 2d unity 0 Answers