Now go ahead and add this script to your player and use the following settings in the inspector. Otherwise if our timer is still running add a velocity in the direction we are dashing multiplied by our dashSpeed. We first check if our dashTimer has exceeded our duration, so we can stop our player from moving, reset our timer back to zero and reset our direction back to no direction. For reach one we set our dash direction.Īfter that we check if we have a direction, if we do then we can kick off our timer to start our dash.
Unity 3d movement update#
In our update method we look for input for the left, right, up and down arrow keys. Next we make sure we initialize our direction to NoDirection since we haven’t used the keyboard input as of yet. We declare a dashTimer which will keep time of whether we have exceeded our duration or not. We then declare a few variables, like dashDuration which is how long we will be dashing for.ĭash speed is the speed or velocity at which the dash will be performed. As well as no direction as when we stop our player from moving, there is no initial direction it will move in. So in the enum we declare up,down,left and right.
Unity 3d movement code#
This will make our code a little easier to read. First we declare a c# enum, which will store our different directions. If (dashDirection = DashDirection.Right)īody.velocity = Vector2.right * dashSpeed īody.velocity = Vector2.down * dashSpeed If (dashDirection != DashDirection.NoDirection)īody.velocity = Vector2.left * dashSpeed
using System.Collections ĭashDirection = DashDirection.NoDirection Let’s in the next version of our script adapt this basic script to control dashing. Which will allow us to control our dash speed. We start off with this basic c# script to allow our player to move. using System.Collections īody.velocity = new Vector2(, - speed * ltaTime) īody.velocity = new Vector2(, + speed * ltaTime) īody.velocity = new Vector2( - speed * ltaTime, ) īody.velocity = new Vector2( + speed * ltaTime, ) Let’s implement this using the new unity input system. Go ahead open up the dash move c# script in visual studio. Click on your player object in the hierarchy and drag the dash move c# script into your components like below. In assets right click and create a new c# script. Then we will add onto the script to allow our dash effect. We now need to come up with a basic script to just get our player to move. You want to change your rigid body 2d to kinematic. Our player sprite might now fall due to gravity. Add a box collider 2d and a rigid body 2d. In your inspector with your player selected start by adding some new components. So to do this drag your player sprite into your hierarchy. We now need to add a kinematic body to our player. Go over to the assets and right click, goto create, sprites and click on square. Then after that we will look at a script for our dash movement. In the next section we will start setting up our project. We also need a basic background so we can see the dash. In our case we will use basic sprites provided by unity like squares. We may need to consider adding some effects as well to our dash so that it doesn’t look too choppy or dull. We also may want some configurable properties so we can modify how our dash behaves.
So when writing our c# dash script we will need to have a delay timer. Some things we need to take into account however is that we need to allow for a duration at which the dash will be allowed. The way in which we can do this is by attaching a body to our player or game object and applying a force to it. So in order to perform a dash we need some instantaneous velocity applied to an object or our player. You should now have a blank project like this. So go ahead and open up unity hub and create a new project called unity 2d dash. We will be looking at doing this in 2d first. To start off create a new 2d unity project. We will look at how a air dash can work and the c# dash script which will make our object or player dash in our game.
Unity 3d movement how to#
In this tutorial you will learn how to make a dash in unity 3d, we will also look at how you can make a unity 2d dash movement effect.