A short clip of the completed assignment in action. You must login to blackboard/panopto for the embedded videos to show!
These videos cover the high-level concepts for this week's topic (the things covered in class on Thu kickoff).
Grab the repo first! There is starter project/code in there.
https://classroom.github.com/a/u8XC69Xv
Out of the box, it should run as below.
The initial code doesn't do much, but has several things set up for you.
BoxCollider2D
component on the player entityPlatformPlayer
script on the player entityPlatformPlayer
)In this stage, you will implement the below funtionality:
Limitations of this Stage
To get started, just try adding gravity. Integrate a constant downward acceleration vector every frame to add more and more downward velocity to the player entity. Recall the formula is just like the formula to integrate velocity to update position.
Here is a video showing an extra example of acceleration due to gravity.
Here is the complete algorithm (with the existing code in it) to implement this Stage. See below for more implementation details.
To use raycast, you need to first set up the Ground/Platform entities
BoxCollder2D
components to the Ground and Platform entities in the sceneIs Trigger
box
Once those steps are complete, you can put in the code to do the raycast and note whether the Player character is grounded or not.
Raycast down from the player to find the closest Platform/Ground
layerMask
SELF_EXTENTS
to calculate where the bottom of the sprite is)RaycastHit2D
object (doc here: https://docs.unity3d.com/ScriptReference/RaycastHit2D.html)
collider
is null, no hitdistance
is very small (like 0.001f), meaning it's right there under youUsing the results of the ground check raycast you can complete the algorithm given above.
Below is a video showing an extra raycasting example with code.
In this stage, you will implement the below funtionality:
Limitations
To implement acceleration-based player movement, replace the line that sets constant velocity based on user input (in the grounded branch) with this algorithm:
To implement jumping, add this algorithm to the end of the grounded branch:
In this stage, you will implement the below funtionality:
Unity's collision system efficiently checks for overlaps in the colliders on all entities that are hooked into the system. To enable collision checks for our Player entity, add a Rigidbody2D
component and set the Body Type
to Kinematic
.
Any time Unity detects overlap between the Player entity BoxCollider2D
and any trigger collider on another entity, it will call the method OnTriggerStay2D
on all components that implement it on the Player entity.
Add the following method to your PlatformPlayer
script to respond to collisions by moving the Player entity out of overlap.
Add more platforms and test out the code.
In the above collision algorithm, it notes that shorter overlap distance is used as an approximation for collision response. This approximation creates an edge case where it can fail to identify whether you hit the side or the top of the platform. If you jump at the corners enough, you can trigger it, and this would be unacceptable to your players.
It is not required, but I encourage you to look closely at the algorithm and consider how to remove that approximation and be precise.