im using this code for my 2D TopDown mobile player movement.but when i move player With UI Image buttons player keep moving.it didnt stop.and also player starting of move he is very slow. the he accelerate his speed.how can i fix this.
i add BoxCollider 2D and Rigibody2D my sprite.the set Gravity to 0.
this is my code.
public class PlayerScript : MonoBehaviour {
public float speed = 150f;
float hInput ;
void FixedUpdate()
{
Move (hInput);
}
public void Move(float horizontalInput){
if (horizontalInput.Equals(1)) {
rigidbody2D.AddForce (Vector2.right);
}if (horizontalInput.Equals(2)) {
rigidbody2D.AddForce (-Vector2.right);
}if(horizontalInput.Equals(3)) {
rigidbody2D.AddForce (Vector2.up);
}
if(horizontalInput.Equals(4)) {
rigidbody2D.AddForce (-Vector2.up);
}
}
public void StartMoving(float horizontalInput){
hInput = horizontalInput;
}
}