- Home /
How can I move the character like 2D games?
Hello, someone knows how I can make the character move like in the "Dont Starve" or "One Hour One Life" games
Comment
Best Answer
Answer by FlaSh-G · May 22, 2018 at 10:10 AM
Add a Rigidbody2D and a Collider2D (for example, a CircleCollider2D) to your player GameObject. Then, add a script that has this implementation for starters:
private Rigidbody2D rb2d;
public float speed;
private void Awake()
{
rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
// Put input values in a Vector2
var input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
// Clamp the vector to not have a length over 1,
// otherwise running diagonally would be faster
input = Vector2.ClampMagnitude(input, 1);
// Set the velocity of the Rigidbody2D so it moves in that direction
rb2d.velocity = input * speed * Time.deltaTime;
}
The Time.deltaTime
factor is optional, but i personally like speed
to be in meters/second, which this enables.
Of course, feel free to experiment with this script to get a feel for what it does.
Your answer
Follow this Question
Related Questions
Player movement boudaries in 2D 1 Answer
Controller Movement!! 1 Answer
Enemy not moving towards Player 0 Answers
How can I make the EnemyBullet go further than my target? 0 Answers
Changing player's moving direction 0 Answers