- Home /
best way to cast fireball
this may be a dumb question, but since I'm only a beginner, I'll take my chances and I'll just ask:
How do you cast a spell (eg fireball)? I created a simple sphere (where the fireball will be "born") and made it a child of the magic staff my character is holding. I will later make the sphere invisible. I created a simple cube (where the fireball should hit) and made it a child of the enemy's torso. I will later make that cube invisible too. Now I would like the fireball to fly from the sphere to the cube after it's instantiated. I would guess that the best way is to go through Translate method. Or is there a better, more resource efficient way. Anybody...?
Answer by Andres-Fernandez · Jun 02, 2014 at 08:10 AM
As you already know the initial and final position (your staff ball and the cube), you only need to make your fireball translate from the initial to the final position. I suggest using a coroutine and Vector3.lerp to create the translation of the fireball over time. Check the manual on corountines, startcoroutine, and Vector3.Lerp.
As @trumpfan says, it depends on the mechanics. You could also get the direction from the initial and final positions and add a force to the fireball, or you could make the ball keep track of its target and follow it...
I don't know what you mean by "keep track of its target and follow it". could you code it out for me please? of course just in case it's only a few lines, I really don't want to bother you :)
It means that it follows the target like the ones in Baldurs Gate. You could move, but the projectile changed its direction to aim at you (cursed basilisk spits).
Depending on what you want, code will be different.
If you just want an object to go from point A to point B use a coroutine like this one:
IEnumerator ThrowFireball(Vector3 initialPosition, Vector3 finalPosition, float speed) {
// First get the time of the translation based on the distance between the two points:
float timeToReachTargetPosition = Vector3.Distance(initialPosition, finalPosition) / speed;
// Then start the translation:
float time = 0.0f;
while (time < 1) {
time += Time.deltaTime / timeToReachTargetPosition; // Get the current time gap
transform.position = Vector3.Lerp(initialPosition, finalPosition, time); // Set the position accordingly
yield return null;
}
// Once time equals 1 the ball has reached the final position.
// $$anonymous$$ake your target explode or substract some HP or whatever...
}
This one doesn't follow the target if the target moves away from it. if you want it to follow the target (in case it flees back),you have to update the final position before the lerp with something like:
finalPosition = target.transform.position;
Although that will require to recalculate the time too.
Answer by trumpfan · Jun 02, 2014 at 08:41 AM
It depends on the specific mechanics of the fireball, is this a set-piece that will always spawn and impact the same place like a scene in a movie?
Can the fireball miss?
The lerp method suggested by Andres would work well for an RTS-like projectile, or a set-piece.
no, it's not gonna be an RTS game. I would like it to be a super cool RPG game one day :) so the answer is yes, the fireball ought to miss time to time. what would you suggest?