Jump to content
  • Advertisement
Sign in to follow this  
Ovicior

Unity How Do You Do 2D Movement In Unity?

This topic is 955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

Wondering how you would do 2D movement in Unity. I'm making a basic pixel RPG to learn more about 2D programming and games. I can't seem to find a good and simple movement system though. I've been looking at this code, and it seems much more complex than 3D stuff. Can anyone show me some code or give me some ideas on how to do this? Please keep in mind I'm very new to Unity. I'd like a movement system similar to Risk of Rains. Basic but working.

Share this post


Link to post
Share on other sites
Advertisement

https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz

 

He's making something complex. Hence the complexity.

 

If you just want basic platform mechanics then the Unity tutorials can get you started:

 

https://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers?playlist=17093

 

Mario-esque controls require a more specialized implementation, and that's a more general problem than just Unity.

 

Attached is an example project with some more refined controls.

Share this post


Link to post
Share on other sites

There is an sample 2D movement controller bundled with unity. This uses rigidbody.AddForce(Vector2), but to me it feels clunky.

 

My preferred method is to use rigidbody.velocity, this creates responsive movement. Have floats that correspond to your sprite's 2D movement speeds, and use Input.GetAxis("horizontal"/"Vertical") to move your character.

 

Short example:

float dx = Input.GetAxis("Horizontal");
float dy = Input.GetAxis("Vertical");

GetComponent<Rigidbody2D>().velocity = new Vector2(dx * playerSpeedX, dy * playerSpeedY) * Time.deltaTime;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!