Week 2 – Code

//Version 0.1 15/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
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
float jmp = Input.GetAxis (“Vertical”);
// Makes use of the vertical axis for jumping
//_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
_crb.velocity = new Vector3 (fr_bk * runSpeed, _crb.velocity.y, 0);
_crb.velocity = new Vector3 (_crb.velocity.x, 0, in_out * runSpeed);
if (_crb.velocity.y == 0)
_crb.velocity = new Vector3 (_crb.velocity.x, jmp, _crb.velocity.z);
else if (_crb.velocity.y > 1)
_crb.velocity = new Vector3 (_crb.velocity.x, 0, _crb.velocity.z);
// 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
// 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, 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

}
}

Advertisements

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