Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bullet spread not working at all angles

Discussion in 'Scripting' started by newjerseyrunner, Sep 20, 2017.

  1. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Hello, I'm working on a game that has a number of guns that are designed to have spreads. I want the spread to only affect the y axis, meaning that instead of getting a cone of projectiles, you end up with more of a flat plane.

    When the player is facing one direction, the spread works perfectly, but in the opposite direction the spread seems to go to almost zero, and angles in between seem to integrate between the two poles. Why is this? I'm assuming that it's my code.

    Here is the method that I'm using to create my bullets. The "firepoint" object is just the location where the bullets are spawned from
    Code (CSharp):
    1. void createNewBullet(float rotationAddition){
    2.         //Get the rotation of the gun, to tell me where the weapon is facing
    3.         Quaternion rotati = transform.rotation;
    4.  
    5.         //Add the angles to create the spread
    6.         rotati.y += rotationAddition;
    7.  
    8.         //Send a bullet out in that direction
    9.         Debug.DrawRay(firePoint.transform.position, rotati * Vector3.forward * 10, Color.white, 1);
    10. }
    The screenshot below shows a symptom of the problem. I took my shotgun and fired it towards the floor in different directions to show where the bullets landed. You can see the correct spread at the top of the screen and a nothing spread at the bottom.
    Screenshot 2017-09-20 16.24.19.png
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    You're modifying a Quaternion. It doesn't work like Eulers.

    If you want to use Euler angles, you can do so with the transform.rotation.eulerAngles reference, modify it by adding degrees, then convert it back with Quaternion.Euler()
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Another option is to use transform.Rotate.
    Code (CSharp):
    1. transform.Rotate(0f, rotationAddition, 0f);
     
    newjerseyrunner likes this.
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Thank you both very much. I understand what I did wrong now, I decided to do the Rotate. I didn't remember that the function existed.
     
    LiterallyJeff likes this.