Hi all,
Trying to make a shotgun spread in a top down shooter, and any shots which go on a negative Y axis are fine, but anything with a positive y axis, the x axis no longer works. As you can see in the attached image, this means I can draw a lovely smiley face, but it's not exactly the effect I'm going for.
![alt text][1]
I'm calculating the initial aim angle as follows;
var worldMousePosition =
Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f));
var facingDirection = worldMousePosition - transform.position;
var aimAngle = Mathf.Atan2(facingDirection.y, facingDirection.x);
if (aimAngle < 0f) {
aimAngle = Mathf.PI * 2 + aimAngle;
}
Next I pass this aim angle into my shotgun, where I try to iterate through and modify the angle for each pellet to give an even spread;
public override void fire(Vector2 aimDirection, float aimAngle) {
var halfPelletCount = pelletCount / 2;
for (int i = -halfPelletCount; i < pelletCount - halfPelletCount; i++) {
var pellet = ProjectilePooler.instance.GetProjectile("Pellet");
pellet.SetActive(true);
pellet.transform.position = this.transform.position;
var spreadValue = pelletSpread * i;
var pelletAngle = (aimAngle + spreadValue) * Mathf.Rad2Deg;
if (pelletAngle < 0f) {
pelletAngle = Mathf.PI * 2 + pelletAngle;
}
var pelletAimDirection = Quaternion.Euler(0, 0, pelletAngle) * Vector2.right;
pellet.GetComponent().fire(pelletAimDirection, launchForce);
}
}
And finally fire the object in the given direction with addforce;
public void fire(Vector2 direction, int force) {
rigidBody2D.AddForce(direction * force);
}
I've tried debugging the shots, and the values of the direction.x all appear correct (that is to say, they look as they should to give a cone of fire), so I'm at a bit of a loss.
I've seen a few other similar questions which suggest changing a setting on the animator (https://answers.unity.com/questions/1220653/rigidbody2d-addforce-impulse-not-working-on-x-axis.html), but I don't have animators on the pellet objects, and the fact that it's only not working when the y value is positive just makes it even more confusing.
Thanks for any help!
[1]: /storage/temp/158337-shotgun-smiley.png
↧