= Vector2.MoveTowards(, targetPosition, speed * ltaTime) TargetPosition = (new Vector3(targetPosition.x, targetPosition.y, 0.0f)) TargetPosition = new Vector2(0.0f, 0.0f)
Start is called before the first frame update Public class PlayerMovement : MonoBehaviour We will use the Input class to get the position of our mouse clicks. We will also use ScreenToWorldPoint to convert our mouse position on screen to a point in our world.
Also we will use ltaTime to make sure our movement is frame rate independent. We now need to use some of the built in Unity functions like the Vector2.MoveTowards method. Next we need a target position variable which will be the position we want our player to move towards. We need to now define a few things, we first of all want to control our player movement speed so we will need a player movement variable. Now open up your new c# script in visual studio. Like below this is going to be our 2d character controller : Add a new component in the inspector called Player Movement. Unity 2D movement top down Write our code for our Unity 2D top down player movement controllerįirst off click on your player game object. You should now have your player in the scene and should look something like this now. Right click in the hierarchy again and create a new game object, rename it and call it Player. Adding our player to our scene: unity top down movement You now have a nice background in my case I have something that looks like this.
In the inspector add a sprite renderer component.Ĭopy the following inspector settings, the most import settings is the position and the order in layer must be -100 so the background renders at the back of all your assets in your game scene.Īdd in your background image into the sprite slot and you are ready to go. On the left hand side in the Unity hierarchy right click and create a empty game object. If you don’t want to follow along on this page you can watch the video version below: Setting up our top down game scene Doing 2D top down movement in Unity for your player can easily be achieved with some basic Unity functions and a small script.įirst we just going to setup our project.