Thursday, October 8, 2015

Unity Side Project: Implementing a dash

Top Down Shooter: How to Add-in a Dash Mechanic

Intro

I started working on a portfolio piece in the Unity Engine.  It's been a lot of fun figuring out this popular engine!

I wanted to write up a quick tutorial on how to implement a dash mechanic in this top-down shooter game I've been working on.  I don't know how to add on an animation, but the Mechanim system should be able to handle it.  I just don't have the art system knowledge just yet to put it in myself. Anyways, on with the tutorial!

Infrastructure of my Scripts

In terms of plumbing, I set up a Player script and a PlayerController script.  The difference is that the logic and hard work are done by the Player script and the PlayerController is called to execute it. This is supposed to have the benefit of modularity, where some other entity can use the Player script for its logic and members, but execute in a different, unique matter.  A good example of this would be an enemy AI.

Note, this is not the best way to handle the problem nor is it the most efficient.  This is a solution I came up with studying the Unity references, Unity answers, and other tutorials.  If there's a better way or if something is completely wrong, let me know! I'm doing this to learn.

Step 1: We gotta limit how much we can dash!

We have to make sure players just can't spam dash!  To do this, we want to keep track of the Time.time in a variable.  Then, in your FixedUpdate (I'm using rigidbody physics, so...) you'll want something like this...


Time.time is Unity's time object and dashCooldown will hold our time stamp every time we dash.  I will then minus Time.time - dashCooldown to get a value of how long it's been since I dashed.  If the value is greater than 2.0, it's now okay to start the Dash() co-routine!

Step 2: What the deuce is a Coroutine?


But I wound up using it because while I was experimenting, any changes to the velocity of the rigidbody were instantaneous and confined just to one frame.  Coroutines give me the ability to wait for a specified amount of time using... 

yield return new WaitForSeconds(some float value);

So now, all I have to is write the coroutine with the following steps:
  1. Check to see if the direction button and the dash button were pressed.
  2. If they were, create a new Vector3 with the dash value we're adding to the velocity.
  3. Call the PlayerController's Dash method
  4. Play particles/art stuff
  5. Record the current Time.time (so player's can't spam it!)
  6. Call our WaitForSeconds() function
  7. Then, create a new Vector3 that sets the dash to 0
  8. Finally, send the new Vector3 to the PlayerController's Dash method.

Step 3: Putting it all together now!

On my side, it looks something like this...

I just did it for one button. It's pretty straightforward from this point.

Step 4: But what about the PlayerController?

It should look something like this, if you've also taken the PlayerController/Player route.
Hopefully that was somewhat useful. I assumed a lot of things in terms of teaching. If you have any questions, let me know!



No comments:

Post a Comment