Create a new Projectile
In this section we will create a new projectile for each the Attack Effects created in Create a new Attack Effect. The main characteristic of the projectile is that its trajectory will be upward, and half way up it will fall on its target.
A projectile is simply a GameObject with a component of or inheriting from the Projectile class.
For this new projectile we will start from the FireOrb projectile, in "Auto-Battle Framework/Prefabs/Projectiles". This is simply a Particle System, simulating a fire orb, with the associated Projectile component.
1. Drag the FireOrb prefab to the scene. Rename it to "WitchOrb". Remove its Projectile component.
Drag the FireOrb prefab and rename it to WitchOrb. Remove its Projectile component.
2. Select the WitchOrb object. Let's give it a pink appearance to differentiate it from the original.
- Set the Start Color to pink. We have used FB18CB (hexadecimal).
- In Color over Lifetime, change the gradient colors. We have used for the first one FA00FF, for the second one FF7400 and for the third one FA00FF.
Set the Start Color and Color Over Lifetime of WitchOrb.
3. Drag WitchOrb to a project folder to save a prefab. Be sure to create it as Original Prefab.
Save the orb as an Original Prefab.
4. In the same way we will create another orb. Select WitchOrb in the scene, rename it to WitchOrbBig. Let's give it a red appearance and make it a bit bigger.
- Set the Start Color to red. We have used FB4318 (hexadecimal).
- Set Start Size to 1.2.
- In Color over Lifetime, change the gradient colors. We have used for the first one FF0008, for the second one FF7400 and for the third one FF9C00.
Set the Start Color, Start Size and Color Over Lifetime of WitchOrbBig.
5. Drag WitchOrbBig to a project folder to save a prefab. Be sure to create it as Original Prefab. Then remove the WitchOrbBig object from the scene.
Save the orb as an Original Prefab.
6. Create a new C# script called WitchProjectile. Read the comments of the script to understand how it works.
- Note that the most important method is Movement, in which the position of the projectile must be defined and the inherited OnHit method must be called at the moment of impact.
using UnityEngine;
namespace AutoBattleFramework.Skills
{
/// <summary>
/// Behavior of projectiles created by the Witch character. Its trajectory will be upward, and half way up it will fall on its target.
/// </summary>
public class WitchProjectile : Projectile //Make sure it inherits from the Projectile class.
{
//Intermediate points of the projectile trajectory. Calculated in CalculatePoints.
Vector3 mid1 = Vector3.zero;
Vector3 mid2 = Vector3.zero;
bool calculatedPoints = false; //If the intermediate points have been calculated.
//Approximate height to which the projectile will rise before falling on its target.
public float midHeight = 2f;
float elapsed = 0f; //Time elapsed since the creation of the projectile. This makes the Speed variable inherited from Projectile mean time, not speed.
//The projectile movement method is overwritten.Instead of a straight line, its trajectory will be curved.
//Note that this function is called in the Update.
public override void Movement()
{
//Calculate the intermediate points only once.
if (!calculatedPoints)
{
CalculatePoints();
}
elapsed += Time.deltaTime; //Update the elapsed time.
float i = elapsed / speed; //Value used to interpolate.
//Calculate the position of the projectile on the Bezier curve.
transform.position = GetPointOnBezierCurve(transform.position,mid1,mid2,target.EnemyShootingPoint.position,i);
//Apply the OnHit method of the AttackEffect and the projectile is destroyed.
if (i >= 1)
{
OnHit();
}
}
/// <summary>
/// Calculate the mid points of the Bezier curve.
/// </summary>
void CalculatePoints()
{
var dist = Vector3.Distance(transform.position, target.EnemyShootingPoint.position);
mid1 = transform.TransformPoint(Vector3.forward * dist *0.33f + new Vector3(0, midHeight, 0));
mid2 = transform.TransformPoint(Vector3.forward * dist *0.66f + new Vector3(0, midHeight, 0));
calculatedPoints = true;
}
/// <summary>
/// Get the point of the Bezier curve, using a value to interpolate.
/// </summary>
/// <param name="p0">First position</param>
/// <param name="p1">Second position</param>
/// <param name="p2">Third position</param>
/// <param name="p3">Last position.</param>
/// <param name="t">Interpolation value.</param>
/// <returns>Point on Bezier Curve.</returns>
Vector3 GetPointOnBezierCurve(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
Vector3 a = Lerp(p0, p1, t);
Vector3 b = Lerp(p1, p2, t);
Vector3 c = Lerp(p2, p3, t);
Vector3 d = Lerp(a, b, t);
Vector3 e = Lerp(b, c, t);
Vector3 pointOnCurve = Lerp(d, e, t);
return pointOnCurve;
}
/// <summary>
/// Lerp two vectors given an interpolation value.
/// </summary>
/// <param name="a">First Vector</param>
/// <param name="b">Second Vector</param>
/// <param name="t">Interpolate value</param>
/// <returns>Lerped vector</returns>
Vector3 Lerp(Vector3 a, Vector3 b, float t)
{
return (1f - t) * a + t * b;
}
}
}
7. Select the WitchOrb prefab and add the Witch Projectile component. In its Inspector set Mid Height to 2.
Add the Witch Projectile component to the WitchOrb prefab.
8. Select the WitchOrbBig prefab and add the Witch Projectile component. In its Inspector set Mid Height to 1.
Add the Witch Projectile component to the WitchOrbBig prefab.
9. Select the WitchBasicAttackEffect created in Create a new Attack Effect. In its inspector, attach the WitchOrb prefab to Projectile. Since the Speed variable refers to time in WitchProjectile, set this to 0.5.
[Attach the WitchOrb prefab to WitchBasicAttackEffect.
10. In the same way, select the WitchSpecialAttackEffect created in Create a new Attack Effect. In its inspector, attach the WitchOrbBig prefab to Projectile. Set the speed to 0.5.
[Attach the WitchOrb prefab to WitchBasicAttackEffect.
11. Test the game, the basic attack should launch a pink orb upward and land on its target. The special attack should be red, not go upwards as much as the basic attack and bounce between enemies.
The witch sends pink and red orbs towards her enemies, with a parabolic trajectory.