Curve spells

Hey guys, i want to cast a spell i dont want it to go directly to a target but doing a curve. I thought of creating many gameobjects that would shape a curve and make the spell go through them, but i want it to work on any target distance. Any help?

Right. Lerp is short for “linear interpolation”. This means that given two values, it can gradually move one towards the other. To create a curved motion on a projectile, and have it home in, you can set the projectile to move forwards in the direction it is facing (using something like: transform.position += transform.forward). You can then use lerping on the rotation to gradually curve the projectile towards the target, like so:

transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(target.position-transform.position), 2*Time.deltaTime);

This will gradually rotate the projectile towards the target, and as it is always moving forwards, will produce a curving motion.

This code alone will cause the projectile to move straight forwards if the target is right in front of it, though, so let’s add a little bit extra to ensure it always curves. To do this, we can simply calculate the rotation it should be facing to aim straight at the target, and then add a bit, using similar calculations as before:

transform.rotation = Quaternion.LookRotation(target.position - transform.position) * Quaternion.AngleAxis(30, transform.up);

Multiplying quaternions in this way means you combine the effects, so this rotates the projectile instantly towards the target, then rotates it by 30 degrees, so it will never be facing the target when you first release it. Note that this second bit of code is only used to set the rotation of the projectile in the first frame after you release it, the first bit of code deals with actually creating the curve motion.