- Home /
Question by
Bleakmountain50 · Jun 04, 2018 at 08:19 PM ·
raycastbulletgunsspread
how would i add bullet spread?
I'm looking to add a small amount of bullet spread since its a pistol. heres my code: using UnityEngine; using UnityEngine.UI; public class PistolShoot : MonoBehaviour { public float damage = 10f; public float range = 100f; public float coreDamage = .001f;
public Camera fpsCam;
public ParticleSystem Mussleflash;
public int ammo = 15;
public bool reloading = false;
public GameObject impactEffect;
public float rateOfFire = .25f;
public bool shootCooldown = false;
public Text ammoText;
public float maxBulletSpreadAngle = 15f;
public float hitForce = 30;
public int recoil = 10;
public Vector3 dir;
public int spread = 1;
void Start()
{
dir.x += Random.Range(-spread, spread);
dir.y += Random.Range(-spread, spread);
dir.z += Random.Range(-spread, spread);
fpsCam = Camera.main;
}
void Update()
{
if (reloading == false)
{
ammoText.text = "Ammo: " + ammo;
}
if(reloading == true)
{
ammoText.text = "Reloding...";
}
if (Input.GetMouseButtonDown(0) & shootCooldown == false)
{
Shoot();
}
if (Input.GetMouseButtonDown(0) & ammo <= 0)
{
Debug.Log("Reload!");
}
if (Input.GetKeyDown(KeyCode.R) & ammo < 15)
{
ammo = 15;
reloading = true;
Invoke("Reload", 4);
}
}
void Shoot()
{
if (ammo > 0 & reloading == false)
{
ammo -= 1;
Mussleflash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
RedCoreHealth redHealth = hit.transform.GetComponent<RedCoreHealth>();
if (redHealth != null)
{
redHealth.TakeDamge(coreDamage);
}
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamge(damage);
}
Destructible destructible = hit.transform.GetComponent<Destructible>();
if (destructible != null)
{
destructible.FallApart();
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
shootCooldown = true;
Invoke("Cooldown", rateOfFire);
}
}
void Reload()
{
reloading = false;
}
void Cooldown()
{
shootCooldown = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Pointing 2 empty game objects towards one point? (Raycast?) 1 Answer
Best way to shoot physical bullets? 2 Answers
Raycasr in my fps? 1 Answer
Bullet Speed / Ray 1 Answer