- Home /
Projectile Directionality in an isometric view (2D, vector2)
Hi,
I am making an isometric game and I have come a long way. I am actually now making my boss fights and Although I have had some success I find that I am still not fully understanding how to determine directionality of a vector2 and how to control it properly.
here is an example ability of a boss in my game which works fine:
void FourDirectionalShot(Vector3 initialDirection, int amountOfDirection)
{
for (int i = 1; i <= amountOfDirection; i++)
{
if (i != 1)
{
initialDirection = Vector2.Perpendicular(initialDirection);
}
GameObject projectile = PooledProjectilesController.instance.GetEnemyProjectile(gameObject.name, aoeProjectile);
projectile.GetComponent<enemy_Projectile>().MakeProjectileReady();
projectile.transform.position = projectileLaunchPoint.transform.position;
projectile.GetComponent<Rigidbody2D>().AddForce(initialDirection * projectileSpeed);
SoundManager.instance.PlayCombatSound(gameObject.name + "_shot");
}
}
This ability shoots projectiles in a maximum of 4 directions as every angle after the first one is simply perpendicular to the previous one.
My question is. What would be a smart way to about shooting in 8 different directions for example, or more. How could I for example get a direction that was in between two of the projectiles in previoiusly mentioned ability. I just want to understand how to better play with this and make some more interesting events.
Any help is greatly appreciated.
Answer by SteenPetersen · Aug 09, 2018 at 10:26 AM
Inspired by madks, I took a dive into effective ways to go about this for my game and I came up with a simple and very effective solution. An extension Method.
public static Vector2 Rotate(this Vector2 vector, float degrees)
{
float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);
float vectorX = vector.x;
float vectorY = vector.y;
vector.x = (cos * vectorX) - (sin * vectorY);
vector.y = (sin * vectorX) + (cos * vectorY);
return vector;
}
This way if I for example wish to fire 12 particles in different yet symmetrical directions I can simply write:
// first fire a projectile at player with a direction "dir"
for (int i = 1; i <= 11; i++)
{
// Given that "dir" is the direction to the player
Vector2 direction = dir.Rotate(30f * i);
// fire in the direction above
}
So a very simple way of doing it and has made my life a lot easier.
Thought I'd share
Answer by madks13 · Aug 06, 2018 at 11:28 AM
Umm, since your projectiles are GameObjects, you can use rotations instead to turn the objects towards the desired direction. That way you can just add/sub angles.