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

Question how to make spray pattern

Discussion in 'Scripting' started by hasanefeyavuz34, Aug 7, 2023.

  1. hasanefeyavuz34

    hasanefeyavuz34

    Joined:
    Nov 20, 2021
    Posts:
    1
    i want a spray parrent like in csgo, my fire function below

    Code (CSharp):
    1. public void Shoot(){
    2.         currentAmmo --;
    3.         RaycastHit hit;
    4.         if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)){
    5.             Debug.Log(hit.transform.name);
    6.             Instantiate(bulletPref, bulletSpawn.transform.position, Quaternion.identity);
    7.             Target target = hit.transform.GetComponent<Target>();
    8.             if(target != null){
    9.                 target.TakeDamage(damage);
    10.             }
    11.             if(hit.rigidbody != null){
    12.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    13.             }
    14.             if(hit.collider.tag == "Zombie"){
    15.                 target.TakeDamage(damage);
    16.             }
    17.             GameObject impactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    18.             Destroy(impactGo,2f);
    19.         }
    20.     }
    with this function my bullet always going straight.(i dont want them to go randomly, i want spray pattern)
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Define spray pattern. Draw a picture.
    If you want normal distribution, check Gaussian distribution, you can use a simple numerical technique called Marsaglia polar. You can thus use a regular linear random value (i.e. Random.value) to feed this function which gives you a normally distributed 2D point. With some fiddling you can easily make a shotgun blast pattern, and generate rays from this etc. For any other kind of spray pattern, you need to be more specific.
     
    Bunny83 and Yoreki like this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Ask yourself this: what is a spray pattern, other than the bullet being fired in a "random" direction with some limitations? There is various ways to implement this, depending on what you really want.

    Shooters are one of the most common genres. You will find thousands of tutorials on pretty much anything commonly involved. Did you try googling for "unity weapon spread"? What issues did you have with the results?
     
    orionsyndrome and Bunny83 like this.
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Edit: misread question
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Code (CSharp):
    1.             Vector3 wobble=Random.onUnitSphere*0.1f;
    2.             if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward+wobble, out hit, range))
     
    Last edited: Aug 7, 2023