Day 111 — Completing the Sword Arc Animation and Implementing Camera Follow

Connor Fullarton
4 min readJul 4, 2021

--

Hey and welcome!

The best way to go about this is to actually redo the attack animation by removing frames and only keeping in a certain portion, this would require a bit of testing and some back and forth with a designer you’re working with but luckily I can just tell you what you need.

Head over to your attack animation and delete the frames currently in there. Hit the record button and drag and drop numbers 6–23 for your Reg_Swing sprite except for the ninth one and drag these into your animation.

You should have quite a snappier sword swing now that lasts the same amount of time as your sword arc effect. Now it’s the case of changing the Sword_Arc position value which normally would be trial and error but the values you want are 1.01, -0.09 and -0.25.

That’s not looking too shabby now I’d say! Last thing we need to do now is flip this sprite so that the animation works while facing left so let’s add in this code to our Player script:

private SpriteRenderer _swordArcSprite;void Start()
{
_playerSprite = GetComponentInChildren<SpriteRenderer>();
_swordArcSprite = transform.GetChild(1).GetComponent<SpriteRenderer>();

}
void Flip (bool facingRight)
{
if (facingRight == true)
{
_playerSprite.flipX = false;
_swordArcSprite.flipX = false;
_swordArcSprite.flipY = false;
Vector3 newPos = _swordArcSprite.transform.localPosition;
newPos.x = 1.01f;
_swordArcSprite.transform.localPosition = newPos;
}
else if (facingRight == false)
{
_playerSprite.flipX = true;

_swordArcSprite.flipX = true;
_swordArcSprite.flipY = true;
Vector3 newPos = _swordArcSprite.transform.localPosition;
newPos.x = -1.01f;
_swordArcSprite.transform.localPosition = newPos;

}
}

Luckily we already have a Flip() method so it’s just a case of piggybacking off of that. This time though we’re also flipping the sprite on the Y axis and we’re specifying the position for our X value. We need to do this because the X value isn’t 0, which means that when we flip the sprite it won’t line up right.

Now with all that done you should have a pretty awesome looking sword swing effect!

One last thing to do now is set the Alpha channel for your sword arc to 0 in the sprite renderer, you may notice that there’s a bit of the effect that always follows the player due to it being the first frame of the animation. Setting the Alpha to 0 makes it invisible, when you’ve done that head to the animation window for the Sword Arc and click on the record button. On your first frame set the Alpha channel to 255 and then the last frame back to 0, this makes it so that the effect is only showing when the animation plays.

Setting up Camera Follow

With that all set up is time to get our camera to follow our player character. Let’s start by installing Cinemachine which can be done by searching it in the Package Manager at Window > Package Manager.

With that imported go ahead and create a new virtual camera using the new Cinemachine menu at the top and set its Z position to -10 so that it’s above all the layers and has a good view of the level and player. When you create the Virtual Camera it will automatically add a CinemachineBrain component to your Main Camera but if it hasn’t you can add that in manually from the add component menu and then set your camera as the Live Camera.

Back in your Virtual Camera you’ll find a Follow property that’s looking for a transform of an object to track, add in your Player object here and watch the magic happen.

That’s all it takes! This was nice and easy to do and as you can see above we have a pretty fluid game forming now.

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet