- Home /
shotgun like shooting
Hey everyone, so im new to programming with unity and have been following some tutorials and such to learn. At the moment i am trying to create a shotgun like effect bullet spread within a 2D environment and have run into a bit of a snag.
The bullet effect works great with the code i currently have the issue is the direction the bullets are firing in, they always seem to generally move towards the top left of the screen instead of the direction the 'cannon' is pointing.
for (int i = 0; i < numberOfPellets; i++) {
Vector3 euler = transform.eulerAngles;
euler.z = Random.Range (0.0f, 90.0f);
bulletEmitter.transform.eulerAngles = euler;
GameObject tempBullet = Instantiate (bullet, bulletEmitter.transform.position, bulletEmitter.transform.rotation) as GameObject;
print ("FIRE!");
The code to fire the weapon is a basic if Input.GetButtonDown("Fire1")
hoping someone might be able to help me figure out the direction problem.
I've included a picture for reference.
Also, if you remove the eulerAngles code the cannon will shoot 1 bullet towards the mouse.
Thanks in advance,
Answer by akillingbeck · Aug 25, 2017 at 02:50 PM
Where is the code where you actually move the bullet in a direction?
void Update () {
//Tells the object to move forward at the set speed
transform.Translate(0, speed * Time.deltaTime, 0);
}
Is used to move the bullets in their facing direction, its in the script thats attached to the bullet prefab. Currently its the only code in that script.
That will translate it along the Y axis only (up). You should pass the 'cannon' transform.forward to the bullet when you instantiate. Then use that in the translate ins$$anonymous$$d
Your answer
![](https://koobas.hobune.stream/wayback/20220612133528im_/https://answers.unity.com/themes/thub/images/avi.jpg)