Platformer Week 6

When I first went to start the platformer I was a little nervous as coding is not my strong point but I used videos online along with the PowerPoint to help me. This helped guide me to have good code that is easy to change for the jumping mechanics and let me add extra features to my game such as spikes that damage the player and causes them to respawn. I do feel like this is my most complete game which works as it is the last game I made within this series. This game shows off the skills that I learned of C## over the course of making these four games, I do feel now as I am writing the code I do understand what is going on and can fix bugs without having to google anything anymore. An example of this is now me being able to read errors and understand what is going on like if my code is missing “;” or if I have made a spelling error I can look at the error to identify the line and go back into my code and change it. I wanted my end goal to be a single scene where you can walk about, the sprite flips, spikes kill you and you can double jump.

For my platformer, I used a mix of the provided code and my own as I found it helped me implement the jumping. For the variables, I had to make the basic movement ones like my speed, jumpforce, and moveInput these made it so that later on I can reference them to code the movement. I made it so that when you walk to the left your sprite flips so it looks a lot more natural, to do this I made a check to see if facing right was true, and if so the spritefaces forward, and if it is false the sprite flips and looks the other way. The jumping was a lot more complicated for me to code as I had to look online to try and find a method that lets me use double jumps. To do this I made an empty object that I coded a little circle which I can change the radius of and have mine set to 0.1 and this checks to see if I am touching the ground and if I am then I am able to jump and I have a value called extrajumps and this I can change the amount of and I gave myself one as this means I can double jump. I can change this in the inspector by clicking on the player and changing the extrajumpvalue under the script. They are not much written in my void Start as the only things that need to be done as the game starts are for the extrajumpvalue to be applied for the jumping script and for the player to have his rigidbody2D.

I will go a bit more into detail now about the code for the jumping. Firstly I have a variable called IsGrounded and the code checks to see if that is true and it does this by seeing if the player is touching the WhatIsGround which I set to the layer Ground and gave that layer to all of my tiles. When the code runs true it allows me to jump and use the extrajumpvalue to doublejump and if the code runs false then I cannot jump anymore as I am not touching the ground and I have no extrajumpvalues to allow me to jump further.

Trying to get the jumping working on the game was a big struggle as I could not seem to get it so that you can just double jump so I went online and did some research and found the fix in some code online and implemented it into my own game, this meant I did not have to set the jumps to 100 like I was when it was broken just to make the game playable I could put in an extrajumpvalue and this allowed me to set a number of double jumps and also have regular jumping work as normal. I have credited the help at the bottom.

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

public class playerBev : MonoBehaviour
{
    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;

    private bool facingRight = true;

    private bool isGrounded;
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask WhatIsGround;

    private int extraJumps;
    public int extraJumpValue;

    // Start is called before the first frame update
    void Start()
    {
        extraJumps = extraJumpValue;
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, WhatIsGround);

        moveInput = Input.GetAxis("Horizontal");
        Debug.Log(moveInput);
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

        if (facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if (facingRight == true && moveInput < 0)
        {
            Flip();
        }
    }
    void Update()
    {
        if (isGrounded == true)
        {
            extraJumps = extraJumpValue;
        }

        if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
        {
            rb.velocity = Vector2.up * jumpForce;
            extraJumps--;
        }
        else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
        {
            rb.velocity = Vector2.up * jumpForce;
        }
    }



    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
}

This is the code I used to make it so Spikes “kill” you. To do this I added void OnTriggerEnter2D as this means when the player runs into whatever has this code the script will run. My script has it so that when the “player” marked object collides with whatever his this script attached it causes the scene to restart which gives the effect of the player has died. This was very simple to implicate and I found a helpful video online that showed me how to do this, I enjoy having the scene restart as it means you do not teleport back to the starting position as this does not work as death if you have any objective in the game as anything you have collected you still have and it takes away any difficulty of the game.

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

public class killPlayer : MonoBehaviour
{
    public int Respawn;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag("Player"))
        {
            SceneManager.LoadScene(Respawn);
        }
    }
}

This is Video Gameplay of my character double-jumping, dying to spikes, and “respawning” which restarts the scene. I just included this as a simple visual of what the code does.

Overall I am proud of this game as I feel as though I do understand the code now and was able to fix errors I had with my code and go out and seek fixes. I feel like I have presented a fully working prototype that allows you to do the basic functions of a platformer game.

These are the videos I used to help me:

Leave a Reply

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