Skip to content

Commit

Permalink
Fixes for movement code
Browse files Browse the repository at this point in the history
  • Loading branch information
Joeclinton1 committed Nov 24, 2023
1 parent 34316df commit 8315112
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions docs/tutorials/01-Platformer Tutorial/04-Moving Objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ For `Platform 5`, we want it to rotate in place around the y-axis (yaw), like a

<ScratchBlocks>
{`
GameObjects.setRotation \\( object name [Platform 5] rotation \\(yaw, pitch, roll\\) (([sin v] of (timer)) * [180]) [] [] \\) :: custom
GameObjects.setRotation \\( object name [Platform 5] rotation \\(yaw, pitch, roll\\) (([sin v] of ((timer)*[30])) * [90]) [] [] \\) :: custom
`}
</ScratchBlocks>

Expand All @@ -62,6 +62,8 @@ If you aren't familiar with yaw, pitch and roll, then just just know that yaw ro

![principal axes](media/yaw-pitch-roll.png)

We wanted to rotate our platform around the y-axis, so that means we applied a rotation on the yaw axis. To make it oscillate we used sin motion again, with timer as the input parameter. Multiplying timer by 30 makes it repeat roughly every 6 seconds and multiplying it by 90 outside the sin block, makes it spin quater way around and then back again.

## Moving our enemy towards the player

Our enemy isn't just going to stand there! Lets make our enemy look at the player and then move towards the Player, so that the Player has to react quick or get killed.
Expand All @@ -72,7 +74,7 @@ To point an object towards a position in ChromeEngine we use the `GameObjects.po

<ScratchBlocks>
{`
GameObjects.pointTowardsPosition \\( object 1 name [Enemy] Position [@player_x] [] [@player_z]\\) :: custom
GameObjects.pointTowardsPosition \\( object 1 name [Enemy] Position (@player_x) [] (@player_z)\\) :: custom
`}
</ScratchBlocks>

Expand All @@ -83,7 +85,7 @@ To move an object forward by a step we use the `GameObjects.move` block. Here's

<ScratchBlocks>
{`
GameObjects.move \\( object name [Enemy] steps [0.1]\\) :: custom
GameObjects.move \\( object name [Enemy] steps [0.04]\\) :: custom
`}
</ScratchBlocks>

Expand All @@ -96,15 +98,17 @@ We can constrain the rotation by setting the pitch rotation to 0 AFTER calling `

<ScratchBlocks>
{`
GameObjects.pointTowardsPosition \\( object 1 name [Enemy] Position [@player_x] [] [@player_z]\\) :: custom
GameObjects.pointTowardsPosition \\( object 1 name [Enemy] Position (@player_x) [] (@player_z)\\) :: custom
GameObjects.setRotation \\( object name [Enemy] rotation \\(yaw, pitch, roll\\) [] [0] []\\) :: custom
if <<([abs v] of ((@player_x)-[0]))< [1.4]> and <([abs v] of ((@player_z)-[-20]))< [1.9]>>then
GameObjects.move \\( object name [Enemy] steps [0.1]\\) :: custom
if <<([abs v] of ((@player_x)-[0]))< [1.25]> and <([abs v] of ((@player_z)-[-20]))< [1.85]>>then
GameObjects.move \\( object name [Enemy] steps [0.04]\\) :: custom
`}
</ScratchBlocks>

- `0` and `-20` are the x,z coordinate of the platform
- `1.4` and `1.9` are the width, height of the platform
- `1.25` and `1.85` are a little more than half the width and depth of platform 6.

The code may look a bit complex, but all it means is "if the player's x is within half the width of the platform from the platform's centre's x and the player's z is within half the depth of the platform from the platform's centre's z then the player is within the bounds of the platform and the enemy should attack"

## Wrapping up and Additional Resources

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If the player touches the specified block it sets `_touching?` to true, and adds

## Killing the player if they touch a hazard

We'll use the `Interactions.playerIsTouching?` block to check if the player is touching any of the hazards and if so add `hazard` to the `ìnteractions` list. Then we'll make it so that if `interactions` contains hazard or the player y is below the level of the ground we reset the player back to the start and play a death sound effect.
We'll use the `Interactions.playerIsTouching?` block to check if the player is touching any of the hazards and if so add `hazard` to the `ìnteractions` list. Then we'll make it so that if `interactions` contains hazard or the player y is below the level of the ground plus a little bit, we reset the player back to the start and play a death sound effect.

Here's the code to do this:

Expand All @@ -28,7 +28,7 @@ Here's the code to do this:
_touching? = Interactions.playerIsTouching? \\( object name [Spike 1] label [hazard] \\) :: custom
_touching? = Interactions.playerIsTouching? \\( object name [Spike 2] label [hazard] \\) :: custom
_touching? = Interactions.playerIsTouching? \\( object name [Enemy] label [hazard] \\) :: custom
if <<[interactions v] contains [hazard] ?> or <(@player_y) < (@ground_y)>> then
if <<[interactions v] contains [hazard] ?> or <(@player_y) < ((@ground_y) + [1])>> then
set [@player_x v] to [0]
set [@player_y v] to [1]
set [@player_z v] to [0]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8315112

Please sign in to comment.