Pages

Tuesday, September 16, 2014

Simple Third Person Character Movement Part II (Jumping, sprinting, falling and landing) : Unity 3D

Hello, Welcome readers to The second part of the tutorial of Third person character movement we started earlier. In this Part we will be working on sprinting, jumping and Landing System. 

 Watch in this video what we are creating and working on in the tutorial.





Since we are resuming our tutorial the first thing will be to download what we created earlier So, please download the file
 http://www.4shared.com/file/y6EyFbNmce/Tutorial_Start.html

It contains the complete finished part one of tutorial along with some new animations like jump, land, midair, and running. You may notice That quality of animation is very poor because i'am a scripter not an animator and this is the best I manage to get.
So let's start By creating a new Project and import this Unitypackage provided above. So, you can see some new animations and you can also hit play to see what we have already done in part one.


Now our first task is to make The player run when pressing sprint button (usually shift button). To make this work First open the animator window and drag the Run animation into the window and make a transition from walk to run with the condition speed > 1.5 like this.


Now create a Transition from run animation back to walk with condition speed < 1.5. To make all this work together we have to just write two lines in our code.
So, open our player script And In the void movement where we set the speed of animator like this -

anim.SetFloat ("speed",1);

Just replace this line with this -

if(Input.GetKey(KeyCode.LeftShift))
            {
            anim.SetFloat ("speed",2);
            }
            else {anim.SetFloat ("speed",1);
            }


So now our whole Function will look like this -

    void movement (float v, float h) {
        if (h != 0f || v != 0f) {
           
            //checking if the user pressed any keys
            rotate(v,h);
           
            //rotates the player in the intended direction
            if(Input.GetKey(KeyCode.LeftShift))
            {
            anim.SetFloat ("speed",2);
            }
            else {anim.SetFloat ("speed",1);}

            //then move the player in that direction
        }
        else {
            anim.SetFloat ("speed",0);
           
            //Stop the player if user is not pressing any key
        }  
    }


Now Hit the Play button to see your player sprinting when Left Shift is pressed. Although it not look like professional games That's because of the crappy animation provided by unity. But you can replace the run animation with our choice.

2. After The Sprinting part is Complete, We will focus on jumping and Landing. As much I know landing is more important part than jumping . The Logic of falling and landing is that we will create a variable grounded which will store if the player is landed or not. And whenever the grounded var become false we will change the animation state to falling and  when the grounded become true again we will change the state to landing and then back to idle.
So our first step is to create that variable. For this purpose we will raycast a ray down to compute the distance between player and ground. So add this to where we declare our variables (above all methods but inside the class brackets)

public float dist;
public bool grounded;
public Vector3 castpos;

You will notice I created a Vector3 castpos because I noticed that sometimes The main Transform of player goes under the surface where player is standing and it will give us wrong information. This is something like an offset to raycast position. And finally, add this in the Void Update


  castpos = transform.position;
  castpos.y += 0.2f;

RaycastHit hit;
        if (Physics.Raycast(castpos, -Vector3.up, out hit, 100.0F))
             dist= hit.distance;

if (dist < 0.5f) 
     {
       grounded = true;
    } 

    else 
    {
       grounded = false;
    }

You can now check the functionality by dropping from height and ensure that it works properly. now add this line also in function Update -

anim.SetBool ("land", grounded);

I know We never made any bool named land in animator before. Yes, we will make it now So, go back to the animator window and add a new parameter 'land' as a bool.


Now import the Midair animation (It's named Idle_JumpDownHigh_Idle) and the landing animation (it's named Idle_JumpDownLow_Idle) These are mainly jump up and down animation which i cropped for our purpose Into the animator window. And rename them correctly for better understanding.


I am trying to keep it simple but unfortunately we will have to make it some complex for proper functioning. What we will do now is to connect every state (idle, walk and run) to The midair state with condition land = false like this -


And now make a transition from from midair to land with condition land = true. And then finally a transition from land to idle with default condition Exit Time which mean when a state ended it's one loop. If you don't understand what i mean take a look at this -


Now if you play the game you can clearly see some mistakes and malfunctioning of our system such as the player leaves a platform it do not drop down to ground but get stuck at the corner that is because when the grounded become false it immidiately changes to midair without leaving the corner fully like this -


But don't get worried there is an easy solution to this problem, Click on of the Transition we made from idle, walk and run to midair And in the inspector you will see something like a timeline with two dots at the top of that timeline. (See the red circles to see the dots i was talking about)


The distance between these dots the amount of blending between two animations, As to solve our problem just widen the gap between then a bit according to my picture for all Transition. If you are not able to find the good point you can download the complete project (see below) which contains m setted animator controller with perfect parameters.

So now The Landing Part is completed we will do the jumping part. We will first start with the scripting. This is very very easy and simple just add this simple code in Void Update -

if (Input.GetKeyDown (KeyCode.Space)) {
                        anim.SetBool ("jump", true);
                } else {
                        anim.SetBool ("jump", false);
                }


Again i assigned the value of a parameter we do not created yet. So let's go back to animator window and create a bool parameter named 'jump' And drag the two jump animations provided by us. One of them is jumping in place and the other is jumping while walking or running ( Again the Quality of animations is poor cause i'am not a modeler or animator nor I have gaint mocap machines)


Now make a transition from Idle to jump (on spot) with condition jump = true and a return transition with condition Exit time 0. And hit play to check it's working condition which should be good.
Similarly do the same with jumping (while walking and running). Make transition from both walk and run with condition jump = true and a return transition with Exit time 0 But this time also add another condition Of speed according to situation like in return transition to walk speed  < 1.5 and In run speed > 1.5.


That's All for this tutorial, I hope you like it.You can also download the complete finished project via this link - 


Thanks for reading my blog.


No comments:

Post a Comment