Unlock 3D Mouse Controls in Scratch! ๐Ÿคฉ๐Ÿญ - Raycasting E8 Tutorial

griffpatch ยท Beginner ยท๐Ÿ› ๏ธ AI Tools & Apps ยท3y ago

Key Takeaways

Adds 3D mouse controls to a Scratch project using raycasting

Full Transcript

foreign y Ray scratchers this is episode 8 of our raycasting series and by popular demand we are going to add Mouse control and support for looking up and down yes this is so cool making our game feel so much more immersive but this is going to be hard to code right well luckily for us no no it's not it's ridiculously straightforward looking up and down in a raycaster is just as simple as moving the entire scene up and down the hardest part of that will be catering for the otherwise static backdrop we'll get into that shortly as for the mouse the simplest control scheme is to use the mouse's position to guide our player's rotation the further right or left of the center the mouse moves the faster the player turns with a little practice we can get precise and yet smooth control of our movements but is this the best control scheme well almost I have some more advanced options we can try which I'll reveal later in this video so stay tuned dude for that okay guys are you ready let's get scratching we're gonna begin where we left off in episode seven only make sure to save this as a new copy for this is episode eight now you may have noticed as I record these videos that I'm constantly being rushed by Nanos and franks all very good but they don't half get in the way of what I'm trying to show you so we can temporarily separate off this move Block in the entity Sprite just to stop them running around for now sorry little guys but I assure you it's for the best and we'll need to remember to pop this back in again when we're done right then to work click back into the player Sprite and we'll add basic Mouse control under the player tick script follow that turn right that is clockwise block Mouse X is great as it tells us how far left or right the mouse is but the range from negative 240 to 240 is too large compared to the original turn of just three degrees so we divide it by 60. and then pop that all back together at the top here and that's it smash the green flag awesome that gives us control of the player's turns fast or slow and going for a run this makes for an Uber smooth experience granted it doesn't have the immediacy of dedicated Mouse lock more on what that is later but once you get used to it it works pretty well but once we are able to steer left and right with the mouse you do quickly start missing being able to look up and down too so that will be our next job we'll need a new variable to keep track of where we're looking name it camera Y and make it for all Sprites and we'll simply set it to the value of mouse y the plan is then to scroll the entire view up and down by this amount so click into the pen Sprite where all the drawing is done and find the draw row custom block you can see here this set Y and it's where we calculate the vertical position of each wall strip to scroll it we must offset this that is subtract camera y from the result carefully ensure the whole of this existing expression goes on the left of a new subtract block and subtract from that the new camera y variable great let's give that a run and look at this so cool you can already see it's having the desired effect on the level walls obviously we're not finished because we also need to offset these entities the enemies and stars to keep them in sync with the level the code for them is found under the stamp entity script over here currently the go to XY has left the Y as zero and now we're going to replace that with a subtract block and just drop in canva y into the right hand side that gives us negative camera y there's no need to put a zero on the left if you don't want to and that's better it's working great are we done then no not quite did you notice anything a bit off with this backdrop yeah the gradient backdrop also needs to scroll problem is we drew that on the stage backdrop and unlike pen and Sprite costumes stage backdrops can't be moved around in code well then we'll just have to copy this costume over to a Sprite instead just drag the backdrop costume into the pen Sprite and then we click into the pen Sprite and confirm that the costume is there and ready to use good so since this is no longer a backdrop but a Sprite costume we are going to need to paint or stamp this to the screen and we'll need to do this before drawing anything else as everything else will be layered on top of it so locate the draw custom block this is our entry point previously we began by erasing all to Clear the Stage we'll replace this with our backdrop stamp position it first with a go to XY of zero and the Y needs to be that offset so subtract camera y now pop in a clear graphic effects to ensure it's not tinted by previous brightness effects we may have used switch to that background costume and then set size to 100 so that it fills this screen width finally we stamp this allows our backdrop to still appear behind the raycast level and entity stamps smash that green flag variety oh what have we got here then is this what we hope to see the good news is that the main backdrop can be seen to scroll nicely up and down with the scene but something seriously wrong in the areas above and below are normal view we're getting some color banding the reason of course is that our backdrop costume is only the size of one screen if we move it up and down then we reveal what is behind and that is a mess to fix this we'll need our backdrop to be twice as tall as our screen no problem in the costume editor resize the whole costume to half its size using the canvas middle as a reference then Center it again I find it useful to create a duplicate using the ALT key held before dragging or the copy and paste and now reselect and stretch the middle pair vertically if you hold down the ALT key while you're dragging then it sizes around the middle point useful size up just a little bit larger than the canvas so this middle area is going to be sized up in code to the full screen therefore we want to change the gradient colors to match the original reference Shades to the right there we go that works the same for the floor only for a change I'm going to switch it to a green Hue matching the theme of my maze walls great clean up by removing the reference shapes to the right and all we need to do now is size this up to 200 and we'll do this in code set size to 200 percent sadly it isn't this easy scratch has unhelpfully prevented the size reaching our desired 200 percent as a Sprite would become too large for the screen except for that's what we want so we can work around this of course by switching to the large backdrop costume after sizing to 200 percent and instead switch temporarily to a small costume say a star beforehand let's see oh yes this is far better scratch let's assize a star costume to 200 and once there is happy to switch to the larger backdrop costume as long as we don't then size it again afterwards and we can look up and down and the floor and ceiling look marvelous love that green great job even though as we play we only tend to look up and down a little bit the effect is a whole other level of immersion that brings this 3D to life well I said we'd come back and talk about improving the mouse control to do this it might help us to First add a simple Crosshair create a new Sprite named Crosshair and we can draw it using a centered thick black line with a thin white line centered on top of that then duplicate them and keep them centered by holding the shift as you drag them down select all and then Center them again before duplicating and rotating using shift to snap to 90 degree angles and finally dragged a snap back to the center One Last Time ta-da we are done and it's looking pretty smart script wise when flag is clicked go to X Y of zero zero super easy so this is pretty standard first person shooter stuff but what are your feelings about seeing the Crosshair staying still in the center of the screen while our mouse cursor is still visible moving all over the place it's a little disorientating don't you think well outside of scratch this is overcome by a clever trick called Mouse lock the mouse cursor is hidden and yet Mouse movements still allow for turning Without Limits however in Native scratch we can't pull off this trick yet sadly so instead I prefer to make the Crosshair follow the mouse simply Loop forever and go to Mouse pointer this way the Corsair follows the mouse at all times and we can feel more in control of our aiming while still being able to use the mouse to move around nice okay so one more trick before we finish this episode and that is to fine tune the mouse control Just a Touch what I'd propose is to split the screen into regions and assign each region a different turning sensitivity the idea is that while in a central zone we are not turning much we just want to aim our Crosshair and then in the next Zone we apply a much stronger turn this is the normal turn area so why the outermost Zone that is for SNAP turns where we have to get away really quick to code this will no longer be able to turn by directly Mouse X so drag off the mouse divided by 60. this can be replaced by a new variable we'll name it Mouse turn for this Sprite only and we'll drop it into the left side of this multiply to calculate the value of this variable make a new custom block named get mouse term and this is useful We'll add in an input named ABS Mouse X this will be for the absolute that is the non-negative value of mouse X drop the new Block in above our turn block and as the input variable implied we'll pass in the ABS value of Mouse X and this is useful because now it doesn't matter if we are turning right or left we just have the magnitude or the strength of the turn as a positive number so we set Mouse turn to the ABS Mouse x divided by 60. but because we use the ABS of mouse X you'll find we've lost the ability to turn left no problem if the real Mouse X not the ABS Mouse X the real Mouse X is less than zero then we flip our turn variable set Mouse turn to negative using a subtract Mouse turn okay cool we are back in action let's begin by making the central region less sensitive two turns we'll divide by 100 instead of 60. try that out okay yeah this is good we only get a little movement now for the next band move the last if downwards so that we can place in a new if between if the ABS of mouse X is greater than 50 then we want to turn faster now but we have to be careful not to have a sudden jump in speed of turn we change Mouse turn by and we'll need that divide again but before that a subtract we can subtract the same 50 away from the ABS of mouse X do you see what this does when we get past a value of 50 we subtract 50 from it to bring it back down to zero this we add to the existing turn speed to speed it up to make it turn much faster divide it by 50. cool let's try it out so the middle zone is still less sensitive but move outwards to the right and now we're moving very nice we can have a bit of a run around and it's good but even now I'm having difficulty turning quite fast enough to take all the corners as I'd like and this is why we need a third band duplicate the middle if here and we'll check for an absent Mouse X being greater than 130. this needs balancing by subtracting the same 130 before we divide and that should do it I think finally smash that green flag let's see whoa wow yes that is a lot faster how does it feel on the road yeah this might take a little getting used to now as it turns much quicker if I push it too far yeah I think I'm getting used to it now whoopsie well nearly anyhow we can always tweak those numbers if you prefer to find the best region sizes and speeds that suit you it's pretty straightforward and yeah it's working great now really great and that my friends is where we draw this episode to a close it's been fun coming back to this project and there's plenty more to cover like jumping projectiles yeah if you guys want to see more then please smash that like button now and then comment under the video I need your likes and don't forget subscribe to the channel not to miss any future episodes and fun tutorials oh yeah and if you want to post your finished projects to my scratch Studio then you'll find a link to that in the description under the video so thank you for watching have a great week ahead and scratch on guys foreign

Original Description

In part 8 of "Crazy Simply Raycasters" Scratch tutorial, we learn how to add clever Mouse Controls to allow smooth player turning, as well as enhance our 3d engine to allow us to look up & down! A whole new level of immersion for our exciting 3d Scratch games! This is turning out to be one amazing 3d series :D Don't forget to submit your projects to the studio linked below by commenting the project URL in the studio comments. My tutorials are for all budding game & software developers using Code Blocks. Scratch On guys! ๐Ÿš€ *Boost Your Creativity with Griffpatch* The Griffpatch Academy will take you from "Gamer to Game Creator" Learn more at ๐Ÿ‘‰ https://griffpatch.academy ๐Ÿ‘ˆ ๐Ÿ˜บ Scratch was developed by the Lifelong Kindergarten Group at the MIT Media Lab. See http://scratch.mit.edu ๐Ÿฑ Scratch Studio for this tutorial - https://scratch.mit.edu/studios/32774157/comments ๐Ÿ‘€ Part 1 - https://youtu.be/M1c5TcdITVs ๐Ÿ‘€ Full Episode Playlist - https://www.youtube.com/playlist?list=PLy4zsTUHwGJKolO9Ko_j6IStFIJnTYBul ๐Ÿ‘€ Quick and Fun Tutorials - https://www.youtube.com/playlist?list=PLy4zsTUHwGJKByTn_qV76oTpEYGczqEb_ โค๏ธ Scratch Addons - https://scratchaddons.com/ --------------Video Chapters-------------- 0:00 Intro 1:00 Letโ€™s Get Scratching 1:38 Mouse look โ€“ Left & Right 2:33 Mouse Look โ€“ Up and down 3:46 Offset Entities: Enemies and Stars 4:08 Scrolling Background 5:42 Enlarging the Background 200% 8:16 Crosshair 9:56 Graduated Turning 14:18 Outro
Watch on YouTube โ†— (saves to browser)
Sign in to unlock AI tutor explanation ยท โšก30

Playlist

Uploads from griffpatch ยท griffpatch ยท 0 of 60

โ† Previous Next โ†’
1 Burrow Blitz - A game by Griffpatch
Burrow Blitz - A game by Griffpatch
griffpatch
2 Burrow Blitz Level 1 Speed Run!
Burrow Blitz Level 1 Speed Run!
griffpatch
3 Scratch Messaging Extension (for chrome & firefox)
Scratch Messaging Extension (for chrome & firefox)
griffpatch
4 Scrolling Platformer Tutorial | Part 1 | Get Scrolling
Scrolling Platformer Tutorial | Part 1 | Get Scrolling
griffpatch
5 Scrolling Platformer Tutorial | Part 2 | Gravity
Scrolling Platformer Tutorial | Part 2 | Gravity
griffpatch
6 Scrolling Platformer Tutorial | Part 3 | Platform Detection
Scrolling Platformer Tutorial | Part 3 | Platform Detection
griffpatch
7 Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
griffpatch
8 Scrolling Platformer Tutorial | Part 5 | Collectables
Scrolling Platformer Tutorial | Part 5 | Collectables
griffpatch
9 Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
griffpatch
10 Scrolling Platformer Tutorial | Part 7 | Exit Portal
Scrolling Platformer Tutorial | Part 7 | Exit Portal
griffpatch
11 Scratch 3 | Box2d |  Physics Extension
Scratch 3 | Box2d | Physics Extension
griffpatch
12 Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
griffpatch
13 Zombie Cube | Speedrun challenge
Zombie Cube | Speedrun challenge
griffpatch
14 Scratch 3 | Physics Extension | Version 2
Scratch 3 | Physics Extension | Version 2
griffpatch
15 Top Down Scroller | E1 - Scrolling | Scratch Tutorial
Top Down Scroller | E1 - Scrolling | Scratch Tutorial
griffpatch
16 Top Down Scroller | Part 2 | Enemy AI
Top Down Scroller | Part 2 | Enemy AI
griffpatch
17 Top Down Scroller | Part 3 | Enemy Clones & List
Top Down Scroller | Part 3 | Enemy Clones & List
griffpatch
18 Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
griffpatch
19 Top Down Scroller | Part 5 | You've Won!
Top Down Scroller | Part 5 | You've Won!
griffpatch
20 Cloud Game Tutorial | Part 1 | The Basics
Cloud Game Tutorial | Part 1 | The Basics
griffpatch
21 Cloud Game Tutorial | Part 2 | Encoding and Decoding
Cloud Game Tutorial | Part 2 | Encoding and Decoding
griffpatch
22 Scratch 3 Dev Tools Browser Extension by griffpatch
Scratch 3 Dev Tools Browser Extension by griffpatch
griffpatch
23 Cloud Game Tutorial | Part 3 | Multiplayer
Cloud Game Tutorial | Part 3 | Multiplayer
griffpatch
24 Scrolling Platformer Tutorial | Part 9 | Momentum
Scrolling Platformer Tutorial | Part 9 | Momentum
griffpatch
25 Scrolling Platformer Tutorial | Part 10 | Wall Jumping
Scrolling Platformer Tutorial | Part 10 | Wall Jumping
griffpatch
26 Cloud Game Tutorial | Part 4 | Auto Game Joining
Cloud Game Tutorial | Part 4 | Auto Game Joining
griffpatch
27 Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
griffpatch
28 Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
griffpatch
29 Griffpatch's Live Stream
Griffpatch's Live Stream
griffpatch
30 MMO Platformer Speedrun Challenge
MMO Platformer Speedrun Challenge
griffpatch
31 Appel Platformer | Scratch Game Blog
Appel Platformer | Scratch Game Blog
griffpatch
32 Cloud Game Tutorial | Part 5 | Smooth Movement
Cloud Game Tutorial | Part 5 | Smooth Movement
griffpatch
33 How to Design a Level for Appel
How to Design a Level for Appel
griffpatch
34 Fastest Appel Speedruns | Jan 2021
Fastest Appel Speedruns | Jan 2021
griffpatch
35 Tile Scrolling Platformer (Mario) | 1. Setup
Tile Scrolling Platformer (Mario) | 1. Setup
griffpatch
36 Tile Scrolling Platformer | 2. Grid List
Tile Scrolling Platformer | 2. Grid List
griffpatch
37 Tile Scrolling Platformer | 3. Tile Collisions
Tile Scrolling Platformer | 3. Tile Collisions
griffpatch
38 Tile Scrolling Platformer | 4. Platforming Scripts
Tile Scrolling Platformer | 4. Platforming Scripts
griffpatch
39 Tile Scrolling Platformer | 5. The Level Editor
Tile Scrolling Platformer | 5. The Level Editor
griffpatch
40 Tile Scrolling Platformer | 6. Level Codes
Tile Scrolling Platformer | 6. Level Codes
griffpatch
41 Tile Scrolling Platformer | 7. Drop Through Platforms
Tile Scrolling Platformer | 7. Drop Through Platforms
griffpatch
42 How to make Physics in Scratch | Full Tutorial
How to make Physics in Scratch | Full Tutorial
griffpatch
43 Tile Scrolling Platformer | 8. Enemy Clones
Tile Scrolling Platformer | 8. Enemy Clones
griffpatch
44 Simple Pathfinding Tutorial
Simple Pathfinding Tutorial
griffpatch
45 Tile Scrolling Platformer | 9. Enemies in Level Editor
Tile Scrolling Platformer | 9. Enemies in Level Editor
griffpatch
46 Simple Maze Generation | Scratch Tutorial
Simple Maze Generation | Scratch Tutorial
griffpatch
47 Tile Scrolling Platformer | 10. Mystery Blocks
Tile Scrolling Platformer | 10. Mystery Blocks
griffpatch
48 Simple Car Steering Simulation | Scratch Tutorial
Simple Car Steering Simulation | Scratch Tutorial
griffpatch
49 Simple Background Scrolling Tutorial
Simple Background Scrolling Tutorial
griffpatch
50 Tile Scrolling Platformer | 11. Death, Background & Sound
Tile Scrolling Platformer | 11. Death, Background & Sound
griffpatch
51 Mid Week Update & Channel Membership
Mid Week Update & Channel Membership
griffpatch
52 Simple Grid List Tutorial with Image Scanning
Simple Grid List Tutorial with Image Scanning
griffpatch
53 Tile Scrolling Platformer | 12. Auto Arranging Tiles
Tile Scrolling Platformer | 12. Auto Arranging Tiles
griffpatch
54 Code a Fun Space Shooter Game ๐Ÿš€ | 1. Move & Shoot | Scratch Tutorial
Code a Fun Space Shooter Game ๐Ÿš€ | 1. Move & Shoot | Scratch Tutorial
griffpatch
55 Space Shooter 2 - Enemy Collisions
Space Shooter 2 - Enemy Collisions
griffpatch
56 Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
griffpatch
57 Space Shooter 3 - Camera Shake & FX
Space Shooter 3 - Camera Shake & FX
griffpatch
58 Tile Scrolling Platformer | 14. Sloping Tiles
Tile Scrolling Platformer | 14. Sloping Tiles
griffpatch
59 Pathfinding a Maze using Lists
Pathfinding a Maze using Lists
griffpatch
60 Tile Scrolling Platformer | 15. Crouch & Slide
Tile Scrolling Platformer | 15. Crouch & Slide
griffpatch

Related Reads

๐Ÿ“ฐ
The Modern Browser Testing Stack: AI, CI, Human Review, and the Cost of Maintenance
Learn how to build a modern browser testing stack using AI, CI, and human review to reduce maintenance costs
Dev.to ยท Simon Gerber
๐Ÿ“ฐ
Build an AI Error Explainer in Python
Learn to build an AI-powered error explainer in Python to turn stack traces into actionable debugging JSON
Dev.to AI
๐Ÿ“ฐ
I Built 4 Interactive Engineering Tools You Can Run in Your Browser
Learn how to build interactive engineering tools that run in your browser, making complex concepts more engaging and accessible
Dev.to ยท Dhananjay kumar Seth
๐Ÿ“ฐ
Teaching Smarter with AI: How Teachers Can Save Time, Create Better Lessons, and Personalizeโ€ฆ
Learn how AI can help teachers save time, create better lessons, and personalize learning for their students
Medium ยท AI

Chapters (10)

Intro
1:00 Letโ€™s Get Scratching
1:38 Mouse look โ€“ Left & Right
2:33 Mouse Look โ€“ Up and down
3:46 Offset Entities: Enemies and Stars
4:08 Scrolling Background
5:42 Enlarging the Background 200%
8:16 Crosshair
9:56 Graduated Turning
14:18 Outro
Up next
Anyword's Performance Boost AI Integration
Anyword
Watch โ†’