[Project Week 11] Spell casting

Fireball spell

This week I will be attempting to make my character able to shoot a spell at a target, I also wanted to fix a bug where to the character would get stuck in its attack animation whenever you move and shoot at the same time.

Firstly I want to make my character not 'freeze' for 5 seconds when you start an attack so I simply changed the integer from 5 second cast time to 1:


My character also jumps a little to the left and right when casting which I would like to fix as it looks clunky. To fix this I had to individually select all the attack animation frames and change the pivot points to 0.51 for the left attack animation, and 0.47 for the right attack animation:


I also implemented my spells to the project which are fire, ice and lightning:


The size of the spells were quite extensive, almost the size of the actual player, so I changed the pixels size to 300 which is a lot smaller and more fitting.

Next thing I needed to do was make sure my character 'throws' a spell when casting, but if i'm throwing a spell I will also need a target as my spell will just be thrown into the open with no objective. My target system will be an aim and shoot system rather than projectile, so I would lock onto targets and my spell would be directed towards them when cast.

I implemented a skeleton sprite which will be used as a target dummy for testing purposes, I will add functional A.I for the skeleton later in development.

Here's what the skeleton looks like in my scene:


Now for the spell throwing function, here is the function I will be using for casting a spell on targets:


Next I created an array for my spell casting, so instead of having one prefab for each spell I can add every different spell for the same array as they will have the same functionality anyway:


Here is the prefab settings in unity, the different numbers for each prefab is what I will be using to access that specific prefab so for now I will be using 'fireball' in the first prefab which is '0':


Next I added a line of code to instantiate the prefab(fireball) so that it spawns directly on the player when casting a spell, later I will make it so it shoots from the players staff instead. Quaternion remains the fireballs rotation to the players rotation:


At this moment of coding the fireball just spawns directly on the players position, but I will change that so it directs towards a target instead. I can spawn as many fireball prefabs as i'd like.

Now for the spell script, first of all I needed to add a rigidbody, which will help me move the fireball:


Next I added the speed which decides how fast the fireball will move towards the target, which can be changed within the Unity editor itself:


Next the target transform, which will be the target I wish to attack, for this case the target dummy skeleton:


Next I added the rigidbody to my fireball prefab, however just adding it on its own will not work, and the reason is because after you cast the fireball it just falls to the ground, this is due to the default physics settings attached to the rigidbody. Easy fix was to change the gravity on it to 0, here are my settings:


I also added the spell script to the fireball prefab with the speed of 5:


Now I added functionality of the fireball to reach the target, for now I will be adding a specific line of code for debugging purposes which I will change later on, reason for that is because this method is quite extensive on the CPU so it's not ideal, but better for testing purposes:


Then I added code which calculates the fireballs position to the targets position, so say the skeleton moves from one spot to another, this code will recognize this change and recalculate the direction the spell needs to move to:



Next I need to add rotation code for the fireball as with this current code the fireball just stays in one rotation while flying towards the target, this will change the angle based of the target and players position:


Here is what my spell casting currently looks like in my game, as you can see the fireball fires directly towards and its the target and also the fireball rotation changes based on the angle in which the player is shooting on:


Comments