- Home /
How to make shotgun bullet spread?, How to make bullet spread for shotgun?
Please Help! I'm making a trap shooting practice game and I am trying to make my shotgun bullet spread. My Mouse Shoot code looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class MouseShoot : MonoBehaviour
{
public float range = 5000;
public float damage = 10;
public AudioClip fireSoundEffects;
private Camera fpsCam;
private AudioSource source;
void Start()
{
fpsCam = Camera.main;
source = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
source.PlayOneShot(fireSoundEffects);
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
RaycastHit hit;
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, range))
{
if (hit.collider.GetComponent<IDamagable>() != null)
{
hit.collider.GetComponent<IDamagable>().TakeDamage(damage);
}
}
}
}
}
IDamagable code looks like this:
public interface IDamagable
{
void TakeDamage(float damageAmount);
}
Answer by Acdia · Aug 18, 2021 at 06:51 AM
Hi! You shoot the raycast in the direction fpsCam.transform.forward
. So you could add fpsCam.transform.right
and .up after multiplying them with some small random number (-0.1 to +0.1). (You could take the desired angle and use the sine function on it to get the max value for the factor.) This would result in a rectangel for the spray.
Here is the modified code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class MouseShoot : MonoBehaviour
{
public float range = 5000;
public float damage = 10;
public float sprayFactor = 0.1f;
public int numberOfProjectiles = 10;
public AudioClip fireSoundEffects;
private Camera fpsCam;
private AudioSource source;
void Start()
{
fpsCam = Camera.main;
source = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
source.PlayOneShot(fireSoundEffects);
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
for(int i = 0; i < numberOfProjectiles; i++)
{
Vector3 direction = fpsCam.transform.forward;
direction += fpsCam.transform.up * Random.Range(-sprayFactor, sprayFactor);
direction += fpsCam.transform.right * Random.Range(-sprayFactor, sprayFactor);
RaycastHit hit;
if (Physics.Raycast(rayOrigin, direction.normalized, out hit, range))
{
if (hit.collider.GetComponent<IDamagable>() != null)
{
hit.collider.GetComponent<IDamagable>().TakeDamage(damage);
}
}
}
}
}
}
If you want it to be a circle, you can set a maximum value for the spread. Next you get a random number, which is smaller, than that max value (and use it as the factor for the first direction - up or right), square it, and subtract it from the square of the max value. Form the root of the result and you got your second factor, for the other direction (up or right). root(maxValue² - firstFactor²) = second factor.
Hi, can you please change my code for this? I am completely new to unity and C# so I'm not quite sure what to do. Thank you so much!
Sure! But I'll need to do it tomorrow when my brain is awake :)
Thank you so much! This would help a lot, Thank you!
Your answer
Follow this Question
Related Questions
How do I get AddRelativeForce to work? 0 Answers
Shotgun raycast 1 Answer
How to make bullets spread in unity2D? 0 Answers
Raycast shooting with prefab 0 Answers