Week 3 – Version 0.3 Movement Code – Jump

This is version 3 of the movement code. The only modification is that it adds a gizmo to detect if the player is touching the ground or not and allow him to jump.

//Version 0.3 23/2/2017

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour

//movement variables
public float runSpeed;
//will be needed to determine runSpeed, as to modify it
public float walkSpeed;
// used to separate the walking animation from running and define its speed
Rigidbody _crb;
// will be needed to make connection to the rigid body of the object
Animator _anim;
// is a reference to the animator, to allow to change the state of the animation

bool F_Right;
// used to check if the player is facing towards the right. When the game starts it will always be true

bool grounded = false;
// the bool is used to verify if the player is touching the ground

Collider[] _grdCollisions;
// creates an array that will store what the sphere of detection will collide with

float _sphRadius = 0.2f;
// the radius the sphere which will detect the collisions will have

public LayerMask _grdLayer;
// the layer mask will be used to detetect what is on the ground

public Transform _grdCk;
// the transform will be used for the location of drawing the sphere

public float _jmpHeight;
// used to register the jump height

// Use this for initialization
void Start ()

// underneath the variables are connected to the actual components and are assigned their value
_crb = GetComponent<Rigidbody> ();
_anim = GetComponent<Animator> ();

// we make sure underneath that the player will always face right when the game starts. The character, when set up, will have to be set up to face right
F_Right = true;

void FixedUpdate ()
{ // FixedUpdate gets called after physics took place, so it has a timeframe within which is called, best for physics operations

if (grounded && Input.GetAxis (“Jump”) > 0) {
// Checks that the player is on the ground and the jump button has been pressed, so that the object can jump

grounded = false;
// The grounded bool is changed to false so that the object will be able to jump

//_anim.SetBool (“grounded”, grounded); –uncomment when setting up player animations
// Changes the bool in the animator with the one from the jump code

_crb.AddForce( new Vector3(0, _jmpHeight, 0));
// Adds force to the rigidbody on the y axis, so that the player will jump, without adding any force on the other 2 axis

_grdCollisions = Physics.OverlapSphere (_grdCk.position, _sphRadius, _grdLayer);
// OverlapSphere creates a sphere and returns the colliders that intersect that sphere

if (_grdCollisions.Length > 0)
grounded = true;
grounded = false;
// In basic, if the array has at least 1 element in it, it means that it made a collision, and as such the array lenght will be greater than 0, therefore grounded will be true, being on the ground

//_anim.SetBool (“grounded”, grounded); –uncomment when setting up the animations for character
// Sets the bool parameter of grounded, which changes the animation from jump to idle or walk/run and vice-versa

//_anim.SetFloat (“verticalSpeed”, _crb.velocity.y); — uncomment when setting up animations
// Sets the speed of the jumping animation to that of the rigid body velocity, as to allow a proper blend of the animation

float fr_bk = Input.GetAxis (“Horizontal”);
// Detects if the player pushed a button for lateral movement, by connecting the variable to the InputManager

float in_out = Input.GetAxis (“Depth”);
// Makes use of the depth defined axis to move the object in depth, on the z axis

//_anim.SetFloat (“speed”, Mathf.Abs (fr_bk)); –uncomment to connect to animation
// Modifies the float variable, which determines if the player animation is walking or running – it will be added later in the animation window, once all assets are ready

float _snk = Input.GetAxisRaw (“Fire3”);
// Determines if the shift key, which is usually Fire3, is pressed
//_anim.SetFloat (“sneaking”, _snk); –uncomment when connecting animations
// Modifies the variable which determines if the player is sneaking or not

if (_snk > 0) {
_crb.velocity = new Vector3 (fr_bk * walkSpeed, _crb.velocity.y, 0);
_crb.velocity = new Vector3 (_crb.velocity.x, 0, in_out * walkSpeed);
else {
_crb.velocity = new Vector3 (fr_bk * runSpeed, _crb.velocity.y, 0);
_crb.velocity = new Vector3 (_crb.velocity.x, 0, in_out * runSpeed);

// Makes the object move by modifying the rigidbody velocity x of the object. It takes the value returned by the input (-1 to 0 or 0 to 1) and multiplies it with the runspeed). This technique can also be used to make the player move on the y and z axis
// It also verifies if the player is walking or running by checking if the shift key is pressed and modifies the speed accordingly

// Under is the condition on which the player will flip around when facing a certain direction by using the created function Turn
if (fr_bk > 0 && !F_Right)
Turn ();
else if (fr_bk < 0 && F_Right)
Turn ();

// The Turn function will turn the character around, by modifying its z scale value — it may run into problems as it uses the scale atributtes to turn the player, but will be dealt with if they appear
void Turn () {
F_Right = !F_Right;
// It makes sure that the boolean will have the oposite value first as to not run into errors and keep everything going

Vector3 scale = transform.localScale;
// The scale of the character is registered in a vector3

scale.z *= -1;
// The z area of the scale vector is multiplied by -1 to reverse it and also reverse the direction the character is facing

transform.localScale = scale;
// The current scale parameters of the object/character are replaced with the modified ones, mainly the z one, that was modified so the object will flip around



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s