Skip to content

Commit

Permalink
Module 5, first update
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinhsung committed Jun 23, 2024
1 parent e78fec8 commit cf712ba
Show file tree
Hide file tree
Showing 125 changed files with 15,818 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Classnotes/Module-5/5.1.Platformer/Assets/HeroMovement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class HeroMovement : MonoBehaviour
{
private Rigidbody2D mHeroPhysics;
[SerializeField] private float mSpeed = 3f;

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

// Update is called once per frame
void Update()
{
float d = mSpeed * Time.smoothDeltaTime;

if (Input.GetKey(KeyCode.A)) {
transform.localPosition += new Vector3(-d, 0, 0);
// Note: this is updating the position (no physics)
}

if (Input.GetKey(KeyCode.D)) {
Collider2D other = GameObject.Find("ThePlatform - Brown").GetComponent<Collider2D>();
if (mHeroPhysics.IsTouching(other)) { // can only move right when on platform
Debug.Log("Hero is touching: " + other.gameObject);
mHeroPhysics.velocity = new Vector2(mSpeed, 0);
// Note: this is changing velocity
}
}

if (Input.GetKey(KeyCode.W)) {
transform.localPosition += new Vector3(0, d, 0);
// Note: this is updating the position (no physics)
}

if (Input.GetKey(KeyCode.Space)) {
mHeroPhysics.velocity = new Vector2(0, mSpeed);
// Note: this is changing velocity
}

}

void OnCollisionEnter2D(Collision2D other) {
// Note: the Hero's Collider.isTriggered is OFF
// When this is off, will collide with all Collider2D with isTriggered Off
Debug.Log("Hero CollisionEnter:" + other.gameObject.name);
}

void OnCollisionStay2D(Collision2D other) {
Debug.Log("Hero CollisionStay:" + other.gameObject.name);
}

void OnCollisionExit2D(Collision2D other) {
Debug.Log("Hero CollisionExit:" + other.gameObject.name);
}

void OnTriggerEnter2D(Collider2D other) {
Debug.Log("Hero TriggerEnter:" + other.gameObject.name);
}

void OnTriggerStay2D(Collider2D other) {
Debug.Log("Hero TriggerStay:" + other.gameObject.name);
}

void OnTriggerExit2D(Collider2D other) {
Debug.Log("Hero TriggerExit:" + other.gameObject.name);
}
}
11 changes: 11 additions & 0 deletions Classnotes/Module-5/5.1.Platformer/Assets/HeroMovement.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Classnotes/Module-5/5.1.Platformer/Assets/Scenes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf712ba

Please sign in to comment.