This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if player is on the ground | |
public bool IsGrounded() | |
{ | |
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f); | |
} | |
// Runs everytime the player collides with something | |
private void OnCollisionEnter(Collision collision) | |
{ | |
// If player collide with object with 'Win' tag (i.e. Win Zone) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void FixedUpdate() | |
{ | |
// If player needs to jump | |
if (toJump) | |
{ | |
// We add a force to the player's rigid body | |
playerRigidBody.AddForce(Vector3.up * 8f, ForceMode.VelocityChange); | |
// Reset jump (so player cannot jump until they are grounded again) | |
toJump = false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Update is called once per frame | |
void Update() | |
{ | |
// If user press Space and is on the ground | |
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()) | |
{ | |
// Jump is set to true | |
toJump = true; | |
} | |
// Get horizontal and vertical inputs from user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start is called before the first frame update | |
void Start() | |
{ | |
// Getting necessary components and component values for game logic | |
playerRigidBody = GetComponent<Rigidbody>(); | |
distToGround = GetComponent<Collider>().bounds.extents.y; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Game object containing the win panel that shows the winning message | |
public GameObject winPanel; | |
Rigidbody playerRigidBody; | |
float horizontalInput = 0f; | |
float verticalInput = 0f; | |
float distToGround; | |
bool toJump = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class MyPlayer : MonoBehaviour | |
{ | |
// Game object containing the win panel that shows the winning message | |
public GameObject winPanel; | |
Rigidbody playerRigidBody; | |
float horizontalInput = 0f; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MyPlayer: MonoBehaviour | |
{ | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} |