Skip to content

Instantly share code, notes, and snippets.

View cyberjj999's full-sized avatar

Lye Jia Jun cyberjj999

View GitHub Profile
@cyberjj999
cyberjj999 / MyPlayer.cs
Created May 7, 2022 03:44
Becoming A Metaverse Developer Article | Other Methods
// 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)
@cyberjj999
cyberjj999 / MyPlayer.cs
Created May 7, 2022 03:43
Becoming A Metaverse Developer Article | FixedUpdate() Method
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;
}
@cyberjj999
cyberjj999 / MyPlayer.cs
Created May 7, 2022 03:42
Becoming A Metaverse Developer Article | Update() Method
// 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
@cyberjj999
cyberjj999 / MyPlayer.cs
Created May 7, 2022 03:41
Becoming A Metaverse Developer Article | Start() Method
// 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;
}
@cyberjj999
cyberjj999 / MyPlayer.cs
Created May 7, 2022 03:40
Becoming A Metaverse Developer Article | Defining Game Variables
// 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;
@cyberjj999
cyberjj999 / MyPlayer.cs
Created May 7, 2022 03:38
Becoming A Metaverse Developer Article | MyPlayer.cs (after some coding)
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;
@cyberjj999
cyberjj999 / MyPlayer.cs
Created May 7, 2022 03:36
Becoming A Metaverse Developer Article | The default MyPlayer.cs Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPlayer: MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}