Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

One of those requests: Shooting bullet using players rotatio

Discussion in 'Scripting' started by misterG420, Jun 2, 2021.

  1. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Hi there!

    I know, there are a million threads that asked the same question: How do I shoot the bullet in the direction of the 2d player rotation? Still, I tried a few of those suggestions but I am just clueless at the moment on how to make it work.

    If you can find it in your heart to help me out, I'd be eternally grateful!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shooting : MonoBehaviour
    6. {
    7.     public delegate void On500BulletsShot();
    8.     public static event On500BulletsShot on500BulletsShot;
    9.  
    10.  
    11.     [Header("Projectile Settings: ")]
    12.     private int numberOfProjectiles = 1;
    13.     [SerializeField] private GameObject _bulletPrefab;
    14.     [SerializeField] private float _bulletForce = 15;
    15.     [SerializeField] private ParticleSystem _muzzle;
    16.    
    17.     [SerializeField] private Transform _firePoint;
    18.     private const float radius = 1f;
    19.  
    20.     private float _timeBetweenAttacks = 1f;
    21.     private bool _alreadyAttacked;
    22.  
    23.     private Vector3 startPos;
    24.  
    25.     private CamShake _camShake;
    26.     public static int _bulletShotCounter = 0;
    27.  
    28.     private void OnEnable()
    29.     {
    30.         UpgradeAndAchievementManager.onAchievementUnlocked += ModifyWeapon;
    31.     }
    32.  
    33.     private void Awake()
    34.     {
    35.         _muzzle.Stop();
    36.     }
    37.  
    38.     private void Start()
    39.     {
    40.         _camShake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<CamShake>();
    41.         _bulletShotCounter = 0;
    42.     }
    43.  
    44.     private void Update()
    45.     {
    46.         if (Input.GetButtonDown("Fire1"))
    47.         {
    48.             startPos = _firePoint.position;
    49.             SpawnProjectiles(numberOfProjectiles);
    50.         }
    51.     }
    52.  
    53.     private void SpawnProjectiles(int _numberOfProjectiles)
    54.     {
    55.      
    56.        
    57.         float angleStep = 360 / _numberOfProjectiles;
    58.         float angle = 0f;
    59.  
    60.         if (!_alreadyAttacked)
    61.         {
    62.             _camShake.CamShakeFunction();
    63.             _muzzle.Play();
    64.  
    65.             for (int i = 0; i < _numberOfProjectiles; i++)
    66.             {
    67.                 float projectileDirXPosition = startPos.x + Mathf.Sin((angle * Mathf.PI) / 180) * radius;
    68.                 float projectileDirYPosition = startPos.y + Mathf.Sin((angle * Mathf.PI) / 180) * radius;
    69.  
    70.                 Vector3 projectileVector = new Vector3(projectileDirXPosition, projectileDirYPosition, 0);
    71.                 Vector3 projectileMoveDirection = (projectileVector - startPos).normalized * _bulletForce;
    72.  
    73.                 GameObject _bullet = Instantiate(_bulletPrefab, _firePoint.position, _firePoint.rotation);
    74.                 Rigidbody2D rb = _bullet.GetComponent<Rigidbody2D>();
    75.                 rb.AddForce(_firePoint.up * _bulletForce, ForceMode2D.Impulse);
    76.  
    77.                 _alreadyAttacked = true;
    78.                 Invoke(nameof(ResetAttack), _timeBetweenAttacks);
    79.  
    80.                 angle += angleStep;
    81.                 _bulletShotCounter++;
    82.                
    83.                 if(_bulletShotCounter == 200)
    84.                 {
    85.                     if (on500BulletsShot != null)
    86.                     {
    87.                         on500BulletsShot();
    88.                         if(!AchievementSingleton._instance.achievement1unlocked)
    89.                         AchievementSingleton._instance.achievement1unlocked = true;
    90.                     }
    91.                 }
    92.                
    93.              
    94.             }
    95.         }
    96.     }
    97.     private void ResetAttack()
    98.     {
    99.         _alreadyAttacked = false;
    100.     }
    101.  
    102.     private void ModifyWeapon()
    103.     {
    104.         numberOfProjectiles++;
    105.     }
    106.  
    107.  
    108.     private void OnDisable()
    109.     {
    110.         UpgradeAndAchievementManager.onAchievementUnlocked -= ModifyWeapon;
    111.     }
    112. }
    113.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Should be as simple as something like:
    bullet.transform.up = firePoint.up;

    or
    rb.rotation = firePoint.rotation;


    And you already have the AddForce set up it seems like. In fact you already are passing the rotation into the Instantiate call. So what exactly is going wrong that you need help with?
     
  3. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Hey! That's my issue, I think I passed in the rotation from the player into the instantiated bullet but it doesnt rotate it correclty. The bullet spawn all with the same rotation irregardless of the player's rotation.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    It might be that the player controller doesn't ever rotate but instead rotates another GameObject (possibly a child, possibly even somewhere else) that has the visual rotated object on it. Look for that and use that object's transform instead.
     
  5. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Hmmm. interesting idea; the script sits on the player object but that holds a child with the sprite and another child underneath that holds the firepoint. But I think the rotation of the player object should still work for the rotation of the child?
     
  6. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    It is really odd; I traced back the original tutorial that the shooting script was based on (ofc its Brackeys :D) but I run into same issues with the same code. If anyone has an idea, The error can't be in the script then I have the same hierarchy of player objects (changed it so that scripts sit on the player object which also holds the sprite).
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    Run the game, rotate the player object to 45-ish degrees, press pause.

    Now go find what visible thing in the scene is actually rotated.

    That thing is what you need to aim your bullet.

    Now reason if that is the correct transform that you are using for either
    TheRotatedThingysTransform.right
    or
    TheRotatedThingysTransform.up
    to power your bullet.

    Remember
    transform
    is a simple shortcut to where the script is located, so it might not necessarily be that.