Search Unity

Top down shooter spreadshot help

Discussion in 'Scripting' started by Lemaw, Aug 30, 2015.

  1. Lemaw

    Lemaw

    Joined:
    Aug 30, 2015
    Posts:
    10
    How to make a spreading bullet like the ones in shmup games?
    I tried to duplicate and rotate the weapon, but it ends up shooting at the same direction.

    Here's my code.
    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if ((Input.GetAxisRaw("FireHorizontal") != 0.0f || Input.GetAxisRaw("FireVertical") != 0.0f) && canAttack)
    4.         {
    5.             Vector3 shootDirection = new Vector3(Input.GetAxis("FireHorizontal"), 0, Input.GetAxis("FireVertical")).normalized;
    6.             GameObject bulletInstance = Instantiate(projectile, transform.position, Quaternion.LookRotation(shootDirection)) as GameObject;
    7.             bulletInstance.GetComponent<Rigidbody>().AddForce(shootDirection * projectileSpeed, ForceMode.Impulse);
    8.             canAttack = false;
    9.             Invoke("ProjectileDelay", attackDelay);
    10.         }
    11.     }
    12.    
    13.     void ProjectileDelay()
    14.     {
    15.         canAttack = true;
    16.     }
    Thanks.
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I usually have multiple instantiate lines, one for each angle I want the shot to go at.
     
  3. Lemaw

    Lemaw

    Joined:
    Aug 30, 2015
    Posts:
    10
    Can you tell me how to set the rotation angle?
    It seems like everything i do that doesn't have squiggly lines just ends up shooting at the same direction.

    Thanks.
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Again, I don't do it the most efficient way as angles do my head in. I have extra bullet spawn points that I set to the angle needed then when the player has the multi shot power up I instantiate bullets at each of them at the rotation of the spawn point/s. But.... I focus on 2d so I only have a 2d spread to worry about. The only time I didn't use multi spawn points was when my ship shot in the 4 compass points in which case I used the ships position + vector2.up, down, left, right & found the angles to use to get the 90° increments. I still used 4 instantiate lines tho.