I just moved on to animating projectiles, and I’m trying to make a fireball my player deflects arc back to the enemy using iTween. However, I don’t understand how to have the fireball arc towards the enemy, or have the ending node be dynamically set at the enemy’s position.
Is your deflected fireball going to be like a homing missile and pursue the enemy?
Well yeah, but the enemy is staying still. Its just that i need to ensure that the fireball arcs back and hits its sender instead of an arbituary position
Is your fireball movement done right now by moving from start to target position? And does your fireball hit its target via collision? If you caches the caster Transform in the fireball, you can have the fireball change the target position from the player back to the caster when the player deflects it. The problem with iTween is that it has to complete a tween before starting on another one, and generally unreliable for dynamic stuff from my own experience at least, which is why I’m more fond of using .MoveTowards or .Lerp.
When it is first launched, its just a standard script of velocity aimed at the player horizontally, and yes it is hit by collision. How would you perform an arc with a MoveTowards or Lerp?
If you break the movement of an arc to the horizontal and vertical movement, the horizontal movement is very standard and can be done with a simple MoveTowards from the player to the enemy. The hard part of this is the vertical movement. you will have to identify when the horizontal movement is halfway done and reverse the movement. you will also have to do easing to make it look like an arc instead of looking like an upside-down “V”
However, since you are using velocity, the same concept can be applied using forces/velocity to do the arc. Imagine the distance between your player and enemy as 2 halves. The Y velocity will have to scale based on where along the horizontal distance its at. It will scale from 1 to 0 along the 1st half, and 0 to 1 along the 2nd half.
so the code might look something like this:
//in the fireball
public Transform player;
public Transform enemy;
public float speed;
private Rigidbody rigbody;
private Transform trans;
private Vector3 diff;
private Vector3 halfDiff;
private bool reflected;
void Start(){
rigbody = this.GetComponent<RigidBody>();
trans = this.GetComponent<Transform>();
}
public void Shot(){//called when this fireball is shot
reflected = false;
diff = player.position - enemy.position;
}
public void Reflected(){//called when condition to reflect is met
reflected = true;
diff = -diff;
halfDiff = diff * 0.5f;
}
void Update (){
Vector3 tempVel = diff.normalized * speed;
float yVel = 0;
if(reflected){
Vector3 currDiff = trans.position - enemy.position - halfDiff;
float yMultiplier = 1;
if(halfDiff.z > halfDiff.x){
yMultiplier = currDiff.z/halfDiff.z;
} else {
yMultiplier = currDiff.x/halfDiff.x;
}
yVel = speed * yMultiplier;
}
tempVel.y = yVel;
rigbody.velocty = tempVel;
}
Just wondering, could i put that code into a coroutine?
As long as it runs on some sort of fixed interval to check the current distance between the fireball and enemy I guess, although inevitably using rigidbodies will result in inaccuracies if the interval is too great. If absolute precision is needed, I’d recommend converting this concept to use translation.
note: found some mistakes in the script of my previous reply, edited it already.