Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question how to shoot bullets at a radial direction

Discussion in 'Scripting' started by ccgbagnet, Apr 3, 2024.

  1. ccgbagnet

    ccgbagnet

    Joined:
    Oct 3, 2013
    Posts:
    6
    Hi, I'm trying to create a half circle bullet spread (similar to the illustration). I'm not sure how to properly calculate. Right now what I have is a gun that can be aimed so I'm taking it's rotation as a consideration as well and a bullet that shoots at the direction is pointing at.

    Here is the pseudo-code of what I currently have. But now, I don't know how to calculate angles and do a half circle bullet spread with respect to the current direction.

    I've looked into tutorials like this but can't understand enough to apply to what I currently have.

    Code (CSharp):
    1. intprojectileCount = 5;
    2. Quaternion gunRotation = GetGunGlobalRotation();
    3.  
    4. for (var i = 0; i < projectileCount; i++)
    5. {
    6.      Projectile projectile = Instantiate(projectilePrefab);
    7.      projectile.transform.position = gunBarrelPos;
    8.      projectile.rigidbody.velocity = (gunRotation * Vector3.forward) * speed;
    9. }
     

    Attached Files:

  2. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    367
    This can be done in different ways from math perspective, but I would do something like that:
    Code (CSharp):
    1. int projectileCount = 10;
    2. float arcAngle = 90;
    3. Quaternion fromRotationOffset = Quaternion.Euler(0, 0, -arcAngle / 2);
    4. Quaternion toRotationOffset = Quaternion.Euler(0, 0, arcAngle / 2);
    5. Quaternion fromRotation = gunRotation * fromRotationOffset;
    6. Quaternion toRotation = gunRotation  * toRotationOffset;
    7.  
    8. for (var i = 0; i < projectileCount; i++)
    9. {
    10.     float lerpT = i / (projectileCount - 1);
    11.     Quaternion projectileRotation = Quaternion.Lerp(fromRotation, toRotation, lerpT);
    12.     // Do something with rotation
    13. }
     
    ccgbagnet, samana1407 and zulo3d like this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    A couple of corrections to Lekret's nice code:
    Code (CSharp):
    1.     int projectileCount = 10;
    2.     float arcAngle = 90;
    3.     Quaternion fromRotationOffset = Quaternion.Euler(0, -arcAngle / 2, 0);
    4.     Quaternion toRotationOffset = Quaternion.Euler(0, arcAngle / 2, 0);
    5.     Quaternion fromRotation = gunRotation * fromRotationOffset;
    6.     Quaternion toRotation = gunRotation  * toRotationOffset;
    7.  
    8.     for (var i = 0; i < projectileCount; i++)
    9.     {
    10.         float lerpT = (float)i / (projectileCount - 1);
    11.         Quaternion projectileRotation = Quaternion.Lerp(fromRotation, toRotation, lerpT);
    12.         // Do something with rotation
    13.     }
     
    Last edited: Apr 3, 2024
    Lekret and ccgbagnet like this.
  4. ccgbagnet

    ccgbagnet

    Joined:
    Oct 3, 2013
    Posts:
    6
    This works perfectly. @zulo3d is also correct that it should be modifying the Y part. Thank you so much!
     
    Lekret likes this.