nooblot.blogg.se

Unity 3d player controller
Unity 3d player controller






unity 3d player controller
  1. #Unity 3d player controller update#
  2. #Unity 3d player controller code#

Then finally we set the transform position of our player by calling Vector2.MoveTowards with the player current position and target position. We then take those co ordinates to worldPoints the ScreenToWolrdPoint method needs a Vector3 in our case we just pass the x and y position because we are only moving in 2D space. We then set our targetPosition to our mousePosition. So we call Input.GetMouseButtonDown(0) the zero is for the left click on our mouse.

#Unity 3d player controller update#

Next in the update method we need to get our mouse input. Our player initially will also be at 0,0 in our scene so when our game starts our player won’t jump or move. In the start method we just set a start position to 0.0f and 0.0f just to start with a default position.

#Unity 3d player controller code#

Just a quick explanation on how this code works. = 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.

unity 3d player controller

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.








Unity 3d player controller