-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e78fec8
commit cf712ba
Showing
125 changed files
with
15,818 additions
and
0 deletions.
There are no files selected for viewing
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
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
11
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.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.