- Home /
add quaternion value to a transform.position to create a shotgun weapon for a top down shooter2D
Hello, I want to create a shotgun for my game, that instanciates 3 bullets that all go in different rotations. The 3 bullets are instanciated but do not go in different rotations here is an exerpt from my script to analyse:
     if(isWeaponShotgun)
     {
         rotationbullet1 = Quaternion.Euler(0, 0, 20);
         Debug.Log("rotation bullet 1" + rotationbullet1);
         rotationbullet2 = Quaternion.Euler(0, 0, -20);
     }
     // new part of the code
     
         void ShootShotgun()
         {
             Debug.Log("ShtgunShooot");
             GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
             GameObject bullet1 = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation * rotationbullet1);
             GameObject bullet2 = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation * rotationbullet2);
             
             Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
             Rigidbody2D rb1 = bullet1.GetComponent<Rigidbody2D>();
             Rigidbody2D rb2 = bullet2.GetComponent<Rigidbody2D>();
             rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
             rb1.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
             rb2.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
             gunSound.Play();
             currentAmmo--;
             
             Destroy(bullet, range);
             Destroy(bullet1, range);
             Destroy(bullet2, range);
         }
But the bullet rotation does not change ( the
Answer by Pangamini · Oct 12, 2021 at 08:41 AM
Your bullets travel in the direction of the impulse you applied to them, it has nothing to do with their rotation. Try rotating the impulse vector instead Edit: Since you have already rotated your bullets, you could try
 rb.AddForce(rb.transform.up * bulletForce, ForceMode2D.Impulse);
 rb1.AddForce(rb1.transform.up * bulletForce, ForceMode2D.Impulse);
 rb2.AddForce(rb2.transform.up * bulletForce, ForceMode2D.Impulse);
thank you soo much, it worked perfectly well! had some difficulties understanding quaternion and stuff like that! but this simple answer solved my problem. I gave you 2 reward points, hope you like it ;)
Your answer
 
 
             Follow this Question
Related Questions
The best way for working with rotation in 2d game 1 Answer
Rotating by 90 degrees eventually locks 2 Answers
How to store direction so that player faces last direction when joystick is idle 2 Answers
Rotating an Object in Update Function 1 Answer
translate a gameobjects position.y based on another gameobjects position .z 1 Answer
