Thursday, 24 September 2020

Coding 24/9

 Expanding Our Character Controller.

Now that we have a character that can stand with an idle animation and run at different speeds with a walk and run animation, we will expand the player abilities by making it able to jump.
To begin open up the Player script in your script editor.
- We will start by adding some new variables to the top of my script.
- In the variables area, making sure to be above the Start() function but below the opening squiggly. Type the below lines of code.

public float gravity = 9.8f;
public float jumpSpeed = 4f;
private float directionY;

Now we need to check when the player is pressing the Input that will trigger the character to jump.
- In the Update() function below the line “direction = new Vector3…” type the below lines of code.

If(Input.GetButtonDown(“Jump”)){
direction = jumpSpeed;
}

Next we need to apply our gravity once we jump.
- Under the if() statement we just created. But above the line “if(direction.magnitude…” type the following line of code.

direction -= gravity * Time.deltaTime;

Now we need to change the bit in our script which tell our character to move to also now take into account the jump movement.
- To do this first Inside the if(direction.magnitude >= 0.1f) statement, we need to turn our ‘Vector3 moveDir’ variable, which is currently private only to the if statement. Into a public variable.
- At the top of our script where the variables are written above the Start() function write the below line of code.

private Vector3 moveDir;

- Now, back inside the if(direction.magnitude >= 0.1f) statement, we need to find the line starting with “Vector3 moveDir = Quaternion…” and delete the Vector3 at the start.

- Now underneath the line we just changed. Write the below line of code.

moveDir.y = directionY;

Now go back into Unity and play the game. You should notice that if the player is standing still and you press the space bar, the character will not jump. BUT if you run and press the space bar the character will jump.
- To fix this we need to check if the character is standing still.
- To do this at the end of the if(direction.magnitude >= 0.1f) statement, we need to add an ‘else if’ statement.
- To do this after the closing squiggly add in the following two lines of code.

else if(direction.magnitude <= 0.1f){
moveDir = new Vector3(0, directionY, 0);
controller.Move(moveDir * speed * Time.deltaTime);
}

Now play the game again and press the space bar while the character is standing still. The character
should now jump straight up in the air.

Finally, we need to add in the jump animation.
- In the hierarchy click on the Player GameObject and then open the Animator window.
- Make sure you are in the base layer. (You should see the green empty state transitioning to the Blend Tree)
- In your project window find the Animations folder and then find the BasicMotions@Jump01 files.
- Click the drop-down triangle to open it up and then drag and drop the BasicMotion@Jump01 animation into the Animator window.

- Next right click on the Blend Tree animation state and choose ‘Make Transition’.
- Drag the transition arrow down to the Jump animation state.
- Do the same again but going from the Jump animation state to the Blend Tree.
- Now still in the Animator window, click on the Parameters tab and then the plus button to add a new Trigger parameter.
- Rename the new Trigger parameter to Jump.
- Next we need to create the conditions that will make the move Blend Tree transition to the jump animation.
- Click on the transition going between the Blend Tree and the Jump animation states.
- Now in the inspector untick the Has Exit Time toggle.
- Change the Transition Duration to 0.
- Find the Conditions area and click the plus button.
- In the drop-down menu select ‘Jump’. 
- Now we need to go back to the Player script.
- Inside the ‘if(Input.GetButtonDown…)’ statement we need to tell our Animator to trigger the Jump parameter.
- Under the ‘directionY = jumpSpeed;’ line write the below line of code.

myAni.ResetTrigger(“Jump”);
myAni.SetTrigger(“Jump”);

Now PLAY your game. Hit the space bar are you character should now Jump in the air and play the Jump animation.
Now try hitting space bar again while in the air. You should notice a problem where the player jumps again while still completing the first Jump. Lets fix that.

Back in the player script we need to figure out if the character is already on the ground while jumping.
- Open up the Player script.
- At the top of the script in the variables area but above the Start() function write the below
lines of code.

private bool isGrounded;
private RaycastHit hit;
public float distanceToHit = 0;

- Now at the top of you Update() function we need to create Raycast that will look straight down from the character and tell us how far away the object that it is hitting is.
- At the top of the Update() function, under the first squiggly bracket type the flowing lines of code.

if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, Mathf.Infinity))
 {
 if (hit.distance <= 0.1f)
 {
 isGrounded = true;
 }else if (hit.distance >= 0.1f)
 {
 isGrounded = false;
 }
}

This script is checking how far away from the player the ground is and then changing the isGrounded
bool depending on that distance.

Now the last thing we need to do is to check if the player isGrounded before we allow the player to
jump.
- In the Update() function, find the line of code ‘if(Input.GetButtonDown(“Jump”)’
- Change this line so that it looks like the below line of code.

if(Input.GetButtonDown(“Jump”) && isGrounded)

Now PLAY your game and try to jump while pressing the space bar and the character should now
only jump once. 

No comments:

Post a Comment

BSA702 14/7

 arrays and lists Quick and Easy Galaxy painting  great tutorial I found when I was looking for a background for my pitch tomorrow. I want t...