[Intro] Hello Fellow Scratchers!
I got super excited the other day watching a devlog by a creator
named “Game Endeavor”. They presented some awesome sword wielding mechanics,
and the most fantastic AI controlled enemy fights I’d seen for a long time. What’s more, they gave
some great insight into how they had achieved it. Well in that case, I wondered if it might
be possible to have stab at recreating something similar in Scratch! Now that would be amazing!
So todays video will be the first of a number of devlogs following my attempts at bringing these
ideas to Scratch, beginning with reimagining our beloved Scratch Cat, as a sword wielding,
umm… sword wielding… yes cat. No direct affiliation to other sword-clad felines.
[The Scratch Cat Sprites] Right, then my first job was
to get some decent Scratch Cat costumes. For that we can pop over to @scratchcat’s personal
Scratch profile. Their projects are full of interesting Scratch costumes that we can borrow!
What we need now, is a sword! And here’s one a made earlier 😊
You’ll notice Game Endeavor is using that “floating hand” technique to avoid having to
animate his player’s arms! Very cunning… Maybe we pull off the same trick with Scratch Cat?
Oh gosh… perhaps if I widen their shoulders – Ok nice.
And so that we can wave this sword around, without waving the body too… I’ve split the sword and hand
into a separate sprite to the main cat body. The game loop complete with basic movements
scripts will live in the player sprite, we’ve coded them a million times before!
Now to avoid the cursed lagging sprite bug caused by sprites having their own forever loops,
I use a simple broadcast to the sword sprite to bring from sword to the cat,
and then we are perfectly in sync. Excellent. Another bug to avoid is forgetting to edit
the sword to face right rather than up. All free rotating sprite must begin facing this
way as that’s what Scratch expects. Now it will point is the direction we ask it to. 0 for up.
A little offset, and things are looking pretty sweet! Scratchy’s certainly
enjoying their makeover! 😊 [Angling the player’s arm]
So, on to more exciting things! The sword rotation. Referring to the source material
(as I do over and over again, as the devil is in the detail). You’ll notice that their sword is
always held back, pointing away from the target. This is in preparation for a swing attack.
Not only that, but we have a virtual arm to account for, held at 90 degrees to the sword?
We can do the same, by positioning the sword over the player.
Point it towards the mouse-pointer. Rotate left by 90 degrees and move forward
(that’s for the players invisible arm). Then we rotate left again to leave the
sword facing in the desired direction. String these moves all together into one script,
“and drop in a juicy apple”, and our Scratch Cat becomes a veritable fruit ninja!!! Awesome :D
[Sword Swoosh Motion] But look again at the tech demo,
slowed down, you can see their attack animation is so much cooler than that. The sword swooshes
instantaneous in front of us leaving a white arcing motion trail to represent the force
and speed of the action – So cool! And we can do that too. I just mocked
up some trails using large circles, including a smaller second arc to continue the motion.
Then when the space key is pressed, I trigger “attack mode” and show the motion trail sprite.
And what a difference that makes, Wow! Without even moving the sword 😊
It was getting late in the day, but I wanted to quickly complete the sword motion,
so I mocking it up using a list of directions to cycle the sword through.
And it’s kinda funny how cool this looks, even though there’s something very wrong - The
players arm is comically able to rotate 360 degrees in a complete circle, coming back to
the exact spot it began! [Rotation at the Wrist]
So, the next day I studied the motion more closely, and realised I had to limit the arm
rotation to a half circle. What I missed was the sword hand could also rotate from
-90 to 90 degrees relative to the arm, giving that full 360 degrees of motion,
and bringing the sword from one side of the players body, to the other in a beautiful
swiping motion! Yep, that was the key! At this point, having the sword appear
behind the players feet was getting old. And yet we need the sword to go behind the players head…
so to fix this I brought in my sprite laying tutorial code – You must make sure to watch
that one, it’s so useful and is linked under the video, and it made short work of this
problem. Awesome, that’s much better! [Articulated Spring Physics Simulation]
Okay, time to make this motion more realistic. We want the arm to swipe across, overshoot,
and then come back to rest. To achieve this I needed to give the arm rotational velocity, and
emulate a dampened spring physics system (luckily a lot easier than it sounds). Here’s the code:
SWING DIR is the direction of our arm, and SWING VELOCITY is how fast it is rotating.
This starts at 0, not moving. Then we simply set TARGET DIR to the direction we want the
arm to swing to. The 0.1 value determines how powerful the spring action is, and the 0.65 is
how much dampening is applied so it doesn’t keep oscillating back and forward forever!
That’s beautifully simple! And also allows us to easily swing the sword back up again
by setting TARGET DIR back up to the top with a value like -180. It looks fantastic
no matter what direction we are swinging in. And, to avoid having the player walk around
with their sword in the down position, I automatically set the target to bring the
sword back up if the player fails to perform a second sword attack within a short period of time.
That just makes it feel much more natural. Not to mention flipping the player sprite,
and their sword target when the player faces left since their costume and hands
are therefore also flipped. [Animating the Cat]
Now, I haven’t had the cat moving around the screen for a while. And
for good reason… We are in dire need of a reasonably good walk cycle animation!
But man, can I get these Scratch legs to look right? Why is this so hard! In the end I did
my best to follow the same kind of animation as Game Endeavor’s video, but I can’t say I did them
justice! Anyhow – Here they are imported back into Scratch, and at least now we can feel like we’re
able to dance around the apple and do some serious fruit ninja slicing! This is getting so fun!
[Finishing Touches] And wow – Our sword wielding hero is almost
complete! – a few finishing touches; A ghosted shadow sprite,
to keep the player grounded. And a particle sprite, using clones
to emit dust clouds as we skip around the floor! And we are done! Isn’t that fantastic?!? I feel
this character would slot in very well to many types of game, be that a zombie apocalypse, an
RPG, or some crazy epic fruit ninja extravaganza! However, my next goal is to begin experimenting
with the enemy AI showcased in Game Endeavor’s awesome devlog, so do join me next time,
as I try to harness the power of “Context Steering” to bring this apple to life!!!
I gotta say – It’s coming along really well! [Outro]
Guys, I hope you enjoyed this devlog, if you want to see more, then smash that like button and don’t
forget to subscribe to the channel and tick the bell icon to ensure you get notified when the
next exciting episode drops! If anything in this video got your creative juices flowing,
then drop me a comment under the video and share what you are planning to do about it!
Are you gonna try to build something similar in Scratch too? I’d love to hear about it!
And that’s it from me for today, so have a great week ahead, and Scratch on guys!