Search
  • Charles Low

Coding hurdle after hurdle



PLAYER CONTROLLER

===================


public class PlayerController : MonoBehaviour
{

    // Rigidbody class type of variable
    private Rigidbody playerRB;
    public float jumpForce = 12.0f;
    public float gravityModifier = 3.0f;
    private bool isOnGround = true;

    // Note that this boolean variable should be public so that other scripts can refer to this variable as a trigger
    public bool gameOver = false;


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

        // Here we assign the rigidbody component to the variable playerRB. To get the rigidbody component, you need to use the GetComponent method - a call that we are making a method
        // We need a way to connect to the rigidbody of the object since, unlike a component like transform, not all objects have a rigidbody by default
        // To do this we need to get component
        playerRB = GetComponent<Rigidbody>();

        // Here we are calling Physics in the game and then the property gravity
        // Call gravity property of the physics of our game and multiply it by the gravityModifier variable
        // Physics.gravity = Physics.gravity * gravityModifier
        Physics.gravity *= gravityModifier;

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {

            // Because we are using a rigidbody component, it already has a method called AddForce, e.g. like transform.Translate
            // ForceMode.Impule immediately applies the full force that we want to apply. E.g. as compared to another ForceMode, e.g.:
            // ForceMode.Force = Adds a continuous force to the rigidbody using its mass;
            // ForceMode.Acceleration = Adds a continuous acceleration to the rigidbody, ignoring its mass;
            // ForceMode.VelocityChange = Adds an instant velocity change t the rigidbody, ignoring its mass;
            playerRB.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);

            isOnGround = false;
        }
    }
    // If a collision occurs with an object, which in this case would be the ground, the inOnGround bool becomes true and therefore the player can jump again
    private void OnCollisionEnter(Collision collision)
    {

        if (collision.gameObject.CompareTag("Ground"))
        {
            isOnGround = true;
        }

        if (collision.gameObject.CompareTag("Obstacle"))
        {
            Debug.Log("Game Over!");

            gameOver = true;
        }

    }
 
}


OBSTACLE MOVE

===============


Public class ObstacleMove : MonoBehaviour
{
    public float speed = 20.0f;
    public float xBound = -9.5f;
    private PlayerController playerControllerScript;

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

        // This variable is assigned the value of the GameObject called player and we are taking the component called PlayerController (which is a script called PlayerController)
        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();

    }

    // Update is called once per frame
    void Update()
    {

        // If the gameOver variable in the playerControllerScript (which is referring to the script component to the Player called PlayerController) is false, the transform.Translate function will continue to run
        if (playerControllerScript.gameOver == false)

        {

            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }

        else if (transform.position.x < xBound)
        {

            Destroy(gameObject);
        
        }

    }
}



REPEAT BACKGROUND

====================



public class RepeatBackground : MonoBehaviour
{
    public float speed = 3.0f;

    // Declare variable for the width of the box collider of the background object
    private float repeatWidth;

    // Declaring variable for the starting position of the background object
    private Vector3 startPos;

    private PlayerController playerControllerScript;

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

        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();


        // Assigning the starting position to be the current position
        startPos = transform.position;

        // Assigning the repeatWidth variable a value that is = size of the BoxCollider component divided by 2
        repeatWidth = GetComponent<BoxCollider>().size.x / 2;
    }

    // Update is called once per frame
    void Update()
    {

        // Creating a function that if the position of the background reaches the point that is startPos - repeatWidth, its position will reset to the startPos
        if (transform.position.x < startPos.x - repeatWidth)
        {
            transform.position = startPos;


        }

        if (playerControllerScript.gameOver == false)
        {
            // Movement of the background on the x axis
            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
    }
}

SPAWN MANAGER

===============


public class SpawnManager : MonoBehaviour
{
    public GameObject obstaclePrefab;
    public Vector3 spawnPos = new Vector3(38.0f, 0, 0);
    private PlayerController playerControllerScript;


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

        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();

        InvokeRepeating("SpawnObstacle", 1.0f, 1.0f);


    }

    // Update is called once per frame
    void Update()
    {


    }

    void SpawnObstacle()
    {

        if(playerControllerScript.gameOver == false)
        
        Instantiate(obstaclePrefab, spawnPos, obstaclePrefab.transform.rotation);

    }

}




4 views0 comments

Recent Posts

See All