Week 3 – Version 0.2 Movement Code

This is the second version of the movement code.

I took out the jumping mechanic out of it, as adding velocity to the RigidBody on the y-axis would make it continue doing that while the button was pressed, without stopping. I did try to put a limit on the y-axis, but that would not work, as depending on the level design, the limit would need to be changed dynamically and would overcomplicate the programming.

//Version 0.2 20/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

// 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

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 changes the scale values of the character, but will be dealt with if they appear
void Turn () {
F_Right = !F_Right;
// It makes sure that the boolean will have the opposite 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s