- Home /
Fanning out projectiles depending on direction
Hi!
I'm trying to figure out how to make my multiple projectile skill fan out properly (in a 2D game) depending on the direction player is facing. Right now I have this:
bool alternate = false;
for (int i = 0; i < amtOfProjectiles; i++)
{
GameObject projectileInstance = Instantiate(projectile, playerPos, Quaternion.identity);
Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 direction = (target - playerPos).normalized;
direction.z = 0;
if(alternate){
target.y += i;
}else{
target.y -= i;
}
alternate =! alternate;
// Then I add force to the projectile towards the target
}
This works when mouse is facing left or right, but when mouse is up or down (or between) it obviously doesn't work. I already have the direction calculated so I'm sure I have to use that somehow? I just can't figure out how. I'm hopeless with vectors.
Answer by DawidNorasDev · Jun 20, 2018 at 08:45 AM
I think you don't want to increase y value. This would make fanning much stronger on horizontal shooting, and much lower on vertical shooting. (I know, that's your problem here) What you want I think, is increase / decrease direction angle. You can calculate the angle of direction
and than increase it by an amount making new vector.
You can check this question: https://answers.unity.com/questions/46770/rotate-a-vector3-direction.html
Thank you, I figured it out with this. :) if(alternate){ target = Quaternion.AngleAxis(0-(i*5), direction) target; }else{ target = Quaternion.AngleAxis(0+(i*5), direction) target; }