- Home /
Shooting extra arrows at an angle from the first
Hey guys,
I wish to make my arrows in my game under certain conditions fire a couple of extra arrows at an angle from the main one. so basically like the red lines in the diagram below:
It seems however that this is not working, and it is making some weird effect that is making the arrows fly slower than normal depending on where the mouse is on the screen.
what I do to fire one arrow is as follows:
var startPos = projectilePoint.transform.position;
// create a direction vector from Hit pos of the mouse and the projectiles original position
Vector2 direction = new Vector2(mousePosition.x - startPos.x, mousePosition.y - startPos.y);
direction.Normalize();
// Determine the correct angle to turn the projectile
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
// get an arrow from the already exiting arrows or create a new one if they are all in use
GameObject projectile = pooledArrows.GetPooledArrow();
var projectileScript = projectile.GetComponent<Projectile>();
projectileScript.MakeProjectileReady();
projectile.transform.position = projectilePoint.transform.position;
projectile.transform.rotation = Quaternion.identity;
projectile.transform.localScale = new Vector3(1, 1, 1);
projectile.transform.Rotate(0, 0, angle, Space.World);
So this works very well for firing one arrow. What I wanted to do then was basically determine 2 new directions by doing:
Vector2 direction1 = new Vector2((mousePosition.x - deviation) - startPos.x, mousePosition.y - startPos.y);
direction.Normalize();
Vector2 direction2 = new Vector2(mousePosition.x - startPos.x, (mousePosition.y - deviation) - startPos.y);
direction.Normalize();
Where the deviation would be the how much of an angle I want.
However I think im going about this wrong and I just wanted to hear if anyone had any better ideas.
Answer by Qq0 · Jul 17, 2018 at 03:59 PM
Hey, I think your issue might just be a typo : in the second snippet of code you posted, you have written direction.Normalize(), where it should be direction1 and direction2.Normalize().
Also, I think there may be simpler ways to achieve this, without calculating angles etc; have you considered Transform.Lookat? Then perhaps achieve the 2 new directions with Quaternion.Lerp... although that could be tricky.
Hope this helps!
It was the typo silly me, thanks for taking a look! =)