IN-DEPTH POST #4 – PROGRAMMING FOR VIDEOGAMES

In-depth Post #4 – Programming For Videogames

Justin C

Some Questions I will be answering

What has been my most difficult mentoring challenge so far?  Why?

What is working well for me? Why?

What could be working better?  How can I make sure this happens?

So far the relationship between my mentor and I have been excellent and we haven’t had many mentoring challenges. In the beginning, the most difficult mentoring challenge for myself and my mentor was completing the work that Conor assigned to me between our sessions. So far, I have not been held totally accountable for the work I perform between sessions, and sometimes wasn’t able to get through all the “homework” assigned to me by Conor as either it was too difficult for me or I didn’t manage my time as effective as I could have. As my project becomes more difficult completing said homework will become a more adamant task. So far, communication with my mentor has been excellent. The framework of our meetings has worked very well. I state my session objectives at the start of the meeting, and we spend the majority of our time addressing these. He responds to my questions and expands on topics when I ask him to. In the last bit of our sessions, I ask Conor for something I can focus on for the next two weeks; these past weeks, it was expanding my knowledge in Unity and starting the programming for my game. He gives me some resources to help me learn the topics at hand such as youtube videos or websites. In our meetings, we generally make relatively few assumptions. Because of the nature of my project, Conor can look at my code whenever I ask a question in order to directly address the problem and observe any changes I make in real-time by viewing the program I am working on through screen-share on zoom. Conor has been wonderful at answering all of my questions and enabling me to manage the project, but if not everything can be covered in a meeting, I am free to write him an email if an issue occurs. Conor is a great mentor with all the knowledge needed to handle my queries and concerns.

Programming my Game:

I will be talking about the code that I have written so far and what each part does. The code will be bolded to avoid confusion on what is code and what is me describing it.

This code says that it is using the Unity Engine to run the game.

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

This is labelling the script as the Player Controller.

public class PlayerController : MonoBehaviour

{

These are all variables. Adding “public” in front of the variable allows it to be seen throughout the whole code. You can define a variable as a “Float” and assign a value to it in a single declaration. For example: float playerspeed = 3 sets the player speed to 3. A boolean or bool is similar to math and can either have a value of true or false. These are all of the variables which I have coded and implemented into my game

public float playerspeed;
public float playerstartspeed;
public float jumpheight;
float walljumpheight;
public float walljumpheightpercent;
float walljumplength;
public float walljumplengthpercent;
bool canjump;
bool canwalljumpleft;
bool canwalljumpright;
public bool hasleftwallleft;
public bool hasleftwallright;
public float minwalljumpheight = 1.5f;
public Transform spawnpoint;

public LayerMask groundlayer;

The “Start” function is called before the first frame update when the game is launched and only then.

void Start()
{
Spawn();
walljumpheight = jumpheight * walljumpheightpercent;
walljumplength = jumpheight * walljumplengthpercent;
}

FixedUpdate is called a set number of times per second and is the function that I used to program my movement. In this code I binded the w and space key to jump, the a key to moving left, and the d key to moving right.

void FixedUpdate()
{
LeftWall();
CanWallJump();
IsGrounded();
Rigidbody2D playerbody = this.GetComponent<Rigidbody2D>();
if ((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Space)) && canjump)
{
playerbody.AddForce(Vector2.up * jumpheight);
}
if (Input.GetKey(KeyCode.A))
{
transform.position -= transform.right * Time.deltaTime * playerspeed;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += transform.right * Time.deltaTime * playerspeed;
}
//walljump
if (Input.GetKey(KeyCode.A) && (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Space)) && canwalljumpleft && !canjump && hasleftwallleft)
{
playerbody.AddForce(Vector2.up * walljumpheight);
playerbody.AddForce(Vector2.right * walljumplength);
hasleftwallleft = false;
//Debug.Log(Vector2.right * walljumplength);
}
if (Input.GetKey(KeyCode.D) && (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Space)) && canwalljumpright && !canjump && hasleftwallright)
{
playerbody.AddForce(Vector2.up * walljumpheight);
playerbody.AddForce(Vector2.left * walljumplength);
hasleftwallright = false;
//LeftWall();
//Debug.Log(Vector2.left * walljumplength);
}

//if (Input.GetKeyDown(KeyCode.S))
//{
// transform.localScale = new Vector3(1, 0.5f, 1);
//}
//if (Input.GetKeyUp(KeyCode.S))
//{
// transform.localScale = new Vector3(1, 1, 1);
//}
}

void IsGrounded()
{
Debug.DrawRay(transform.position, Vector2.down, Color.red);
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 1f, groundlayer);
if (hit.collider != null)
{
if (hit.distance <= 0.6f && hit.distance > 0)
{
canjump = true;
}
else
{
canjump = false;
}
}
else
{
canjump = false;
}
}

This is the code that I wrote to create a wall jump mechanism. It uses Raycasting to detect if the player is touching the wall and allows them to jump.

void CanWallJump()
{
Debug.DrawRay(transform.position, Vector2.right, Color.blue);
Debug.DrawRay(transform.position, Vector2.left, Color.green);
RaycastHit2D hitright = Physics2D.Raycast(transform.position, Vector2.right, 1f, groundlayer);
RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, 1f, groundlayer);
if (hitright.collider != null || hitleft.collider != null)
{
if (hitright.distance <= 0.6f && hitright.distance > 0)
{
canwalljumpright = true;
}
else
{
canwalljumpright = false;
}
if (hitleft.distance <= 0.6f && hitleft.distance > 0)
{
canwalljumpleft = true;
}
else
{
canwalljumpleft = false;
}
}
else
{
canwalljumpright = false;
canwalljumpleft = false;
}
}

void LeftWall()
{
RaycastHit2D hitright = Physics2D.Raycast(transform.position, Vector2.right, 1f, groundlayer);
RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, 1f, groundlayer);
RaycastHit2D hitground = Physics2D.Raycast(transform.position, Vector2.down, 1f, groundlayer);
if (hitleft.collider == null || hitleft.distance > 0.6f)
{
hasleftwallleft = true;
}
if (hitright.collider == null || hitright.distance > 0.6f)
{
hasleftwallright = true;
}
if (hitground.collider != null && hitground.distance <= 0.6f)
{
hasleftwallleft = true;
hasleftwallright = true;
}
}

 

public void Spawn()
{
this.transform.position = spawnpoint.position;
playerspeed = playerstartspeed;
}
}

This is the code that I wrote to respawn the player if they fall into the void or touch an obstacle such as lava. The player will respawn after 0.5 seconds of death.

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

public class Respawn : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D col)
{
col.gameObject.GetComponent<PlayerController>().playerspeed *= 0.5f;
StartCoroutine(respawntimer(0.5f, col));
}
IEnumerator respawntimer(float timer, Collider2D col)
{
yield return new WaitForSeconds(timer);
col.gameObject.GetComponent<PlayerController>().Spawn();
}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *