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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I add spread to my bullets using transform.right?

Discussion in 'Scripting' started by jleven22, Jun 7, 2020.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    400
    To further clarify, right now all of my bullets are shooting perfectly straight using:

    Code (CSharp):
    1.         theRB.velocity = transform.right * speed;
    Here's what that looks like now:
    https://www.instagram.com/p/CBGntcKn6dt/?utm_source=ig_web_copy_link

    How can I add some variance so that my bullet isn't firing perfectly straight, but rather has a cone of bullet spread?

    Thanks!
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    transform.right is vector, so it can't be rotated it will always be right of your local space object.

    Code (CSharp):
    1. Vector3 direction  = Quaternion.Euler(0, yourDirection, 0) *  transform.forward;
     
  3. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    400
    Sorry, I'm a bit of a n00b. This code, can you elaborate on it a little? Where does direction belong?
     
  4. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    You can use something like Random.InsideUnitCircle or Random.InsideUnitSphere to get a random vector and then multiply your output by that. I'd also recommend having a variable you can use to change the scale of that vector (so you can have weapons with more extreme spreads like a shotgun for example). Are you doing 2D or 3D?
     
  5. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    400
    This is 2D, top down (there's an Instagram link of the video in the og post).

    I'm not sure how InsideUnitSphere/Circle applies to my situation? Can you explain a little?
     
  6. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    So when you're making it aim towards the mouse I assume you're just doing a MoveTowards or LookAt or something to that effect, right? Just add the result of the UnitCircle multiplied by a scale/spread float (so your target position would be something like
    mousePosition + (unitCircle * scale)
    ).
     
  7. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    transform.right + new vector(0,random.range(1,-1),0) *speed;
     
  8. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    400
    Actually what I am doing is rotating the parent gameObject so that transform.right send the bullet flying in whatever direction the "firepoint" is.

    I'd really like to retain the transform.right part of this equation, but I don't want it to be strictly right. I'd like for "right" to have some variance.
     
  9. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    400
    Just bumping this - still don't have a solution for adding variability to transform.right
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    A simple way is to make the bullet object rotate upon creation. For example:

    Code (CSharp):
    1. public class Gun : MonoBehaviour {
    2.    public float spread;
    3.  
    4.    void Shoot() {
    5.       GameObject bullet = /*However you get your bullet objects*/
    6.       bullet.transform.Rotate(0f, 0f, Random.Range(-spread, spread));
    7.    }
    8. }
     
    PraetorBlue likes this.
  11. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    This is declaring the direction without directly using transform.right.
     
  12. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    400
    THIS DID IT THANK YOU DEAR LORD BABY JESUS
     
    PraetorBlue likes this.