- Home /
Shoot in proper direction with spread?
I'm trying to make a shotgun in a 2D game. I currently have this:
public void Shoot()
{
int rotZ = -10;
for (int i = 0; i < 3; i++)
{
Instantiate(bulletPrefab, firePoint.position, Quaternion.Euler(0, 0, rotZ));
rotZ += 10;
}
}
this works great for the spread, but completely ignores the rotation of my firePoint. I am aware this is because I replaced firePoint.rotation with rotZ, but what I want is to be able to use rotZ for the spread as well as firePoint.rotation for the direction at the same time. Does anyone know how I might do this? thanks!
Answer by AaronBacon · Dec 11, 2021 at 04:48 PM
A very easy way would be to just have the Bullet script randomly rotate itself slightly on Awake:
⠀
public class Bullet : MonoBehaviour
{
//Awake will be called when the bullet is created
void Awake()
{
transform.Rotate(0f,0f,Random.Range(-10f,10f));
}
}
Though you could also do this when spawning the bullet like so:
⠀
private void Shoot()
{
for (int i = 0; i < 3; i++)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
bullet.transform.Rotate(0f,0f,Random.Range(-10f,10f));
}
}
I just put it on like this:
public void Shoot()
{
int rotZ = -10;
for (int i = 0; i < 3; i++)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
bullet.transform.Rotate(0f, 0f, rotZ);
rotZ += 10;
}
}
and it worked like a charm. Thanks!
Ah right, I think I misunderstood what you were trying to do, but yep that works for having a set spread, which thinking about it is probably better for 2D
anyway, while you're here, do you know how I might go about adding knockback to the player when shooting? The gun is a child object of the player, if that means anything about how it would work
Depends on what kind of knockback you want. Assu$$anonymous$$g the player has a RigidBody Component you can use this to knock them back in the reverse direction of their gun:
⠀
⠀ Rigidbody2D rb2d;
public float blastForce = 10f;
void Awake()
{
rb2d = GetComponent<RigidBody2D>();
}
public void Shoot()
{
int rotZ = -10;
for (int i = 0; i < 3; i++)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
bullet.transform.Rotate(0f, 0f, rotZ);
rotZ += 10;
}
Vector3 knockbackDirection= transform.position - firepoint.position;
direction.Normalize();
rb2d.AddForce(direction * blastForce);
}
⠀ I wrote this code a while back so it might not be exactly what you need, if you need more simple knockback (IE, just push the player backwards horizontally a bit) you can look into Rigidbody2D's AddRelativeForce function
I changed it to modify velocity instead of addForce, as well as added a bool to cancel the player movement as that had been interfering, but it's working exactly how I wanted it to! thanks so much dude, you're awesome!
Your answer
Follow this Question
Related Questions
Making a shotgun shooting 45 degrees to the left of the mouse 1 Answer
Instantiate rotation is not correct, why? 1 Answer
Instantiating prefab of object like it was in the scene 0 Answers
how do I keep each cloned object at a certain point after trigger 0 Answers
Objects Instantiating at wrong position 3 Answers