Soo i have been trying to mess around with the raycast to make it not shoot in the same place no matter how many times you shoot and soo far no sucess (the recoil of the bullet), i have looked several ways to do it but no far none have worked , some help in this problem would be nice
here s the code firing class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Weapon : MonoBehaviour {
private Animator anim;
public int bulletsPerMag = 30;//balas por carregador
public int bulletsLeft = 200;//total de balas qe temos
public int currentBullets;//numero de balas qe temos no carregador
public enum ShootMode
{
Auto,Semi
}
public ShootMode shootingMode;
public float FireRate = 0.1f;//tempo por cada bala
public float range = 100f;
public Transform shootPoint;
float Firetimer;
private AudioSource audioSource;
public AudioClip shootSound;
private bool IsReloading;
private bool shootInput;
private bool IsAiming;
public ParticleSystem muzzflash;
public GameObject hitParticles;
public GameObject bulletsImpact;
public float damage = 20f;
private Vector3 orignalPosition;
public Vector3 aimPosition;
public float adsSpeed = 8f;
public Image Crosshair;
[Tooltip("Set the UI Text for the ammo left")]
public Text ammoLeftText;
public float spreadFactor = 0.02f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
currentBullets = bulletsPerMag;
audioSource = GetComponent<AudioSource>();
orignalPosition = transform.localPosition;
}
// Update is called once per frame
void Update () {
switch(shootingMode)
{
case ShootMode.Auto:
shootInput = Input.GetButton("Fire1");
break;
case ShootMode.Semi:
shootInput = Input.GetButtonDown("Fire1");
break;
}
if(shootInput)
{
if (currentBullets > 0)
Fire();
else if (bulletsLeft>0)
DoReload();
}
if (Input.GetKeyDown(KeyCode.R))
{
if(currentBullets <bulletsPerMag && bulletsLeft>0)
DoReload();
}
if (Firetimer < FireRate)
Firetimer += Time.deltaTime;//adiciona no timecounter
ammoLeftText.text = (currentBullets + "/" + bulletsLeft.ToString());
AimDownSights();
}
private void Fire()
{
if (Firetimer < FireRate || currentBullets<=0 || IsReloading) return;
// Debug.Log("Firedddddd");
RaycastHit hit;
if (Physics.Raycast(shootPoint.position, shootPoint.transform.forward , out hit, range))
{
Debug.Log(hit.transform.name + " found!");
// da spam da hit particula e do impacto da posicao da bala
GameObject hitParticlesEffect = Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
// hitParticlesEffect.transform.SetParent(hit.transform);
//da spam to buraco da bala no impacto da posicao da bala
GameObject bulletHole = Instantiate(bulletsImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
Destroy(hitParticlesEffect, 1f);
Destroy(bulletHole, 2f);
if (hit.transform.GetComponent<HealthController>())
{
hit.transform.GetComponent<HealthController>().ApplyDamage(damage);
}
}
anim.CrossFadeInFixedTime("Fire", 0.01f);//play the fire animator
//anim.SetBool("Fire", true);
muzzflash.Play();
PlayShootSound();
currentBullets--;
Firetimer = 0.0f;//reset fireTimer
}
void FixedUpdate()
{
AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
IsReloading = info.IsName("Reload");
anim.SetBool("Aim", IsAiming);
/* if(info.IsName("Fire"))
{
anim.SetBool("Fire", false);
}*/
}
private void DoReload()
{
AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
if (IsReloading) return;
anim.CrossFadeInFixedTime("Reload", 0.01f);//play reload animation
}
public void Reload()
{
if (bulletsLeft <= 0)
return;
int bulletsToLoad = bulletsPerMag - currentBullets;
// if then first else second
int bulletsToDeduct = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft;
bulletsLeft -= bulletsToDeduct;
currentBullets += bulletsToDeduct;
}
private void PlayShootSound()
{
audioSource.PlayOneShot(shootSound);
//audioSource.clip = shootSound;
//audioSource.Play();
}
private void AimDownSights()
{
if (IsReloading) //Aiming stops when reload
{
IsAiming = false;
}
else
{
if (Input.GetButton("Fire2"))
{
transform.localPosition = Vector3.Lerp(transform.localPosition, aimPosition, Time.deltaTime * adsSpeed);
IsAiming = true;
Crosshair.enabled = false; //Enable Crosshair
}
else
{
transform.localPosition = Vector3.Lerp(transform.localPosition, orignalPosition, Time.deltaTime * adsSpeed);
IsAiming = false;
Crosshair.enabled = true; //Disable Crosshair
}
}
}
}