Player can't move far and reset after motion is over
I'm making a new game about a ball rolling ( like Sonic for example). However, the ball won't move past 1 unit in all directions and it's position is reset to original position after motion inputs are done. The code seems right since I compared it with a tutorial coding. What can I do to fix it , please ?
This is the coding :
public class BallControl : MonoBehaviour { Rigidbody2D rigidbody2d; Vector2 position; float horizontal; float vertical; float speed = 3.0f; // Start is called before the first frame update void Start() { rigidbody2d = GetComponent(); }
// Update is called once per frame
void Update()
{
position = rigidbody2d.position;
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
position.x = horizontal + speed * Time.deltaTime;
position.y = vertical + speed * Time.deltaTime;
rigidbody2d.MovePosition(position);
}
}
Your answer
Follow this Question
Related Questions
Third Person controller 1 Answer
How to Apply Animation to Player 0 Answers
What does the other in OnTriggerEnter/Stay/Exit mean? 0 Answers
Help with TopDownGame and mouse position following 1 Answer
GetKeyUp/GetKeyDown optimization 1 Answer