Hello, I need help with my scripts to change fire rate of the weapon, I still don’t understand coding too much, I’m very new to this, if anyone can help me I would be so grateful.
RaycastWeapon:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class RaycastWeapon : MonoBehaviour
{
[SerializeField]
private GameObject bulletPrefab;
[SerializeField]
private Transform barrelTransform;
[SerializeField]
private Transform bulletParent;
private CharacterController controller;
private PlayerInput playerInput;
private float bulletHitMissDistance = 50f;
private Transform cameraTransform;
private InputAction shootAction;
public ParticleSystem muzzleFlash;
public ParticleSystem hitEffect;
public TrailRenderer tracerEffect;
private void Awake()
{
controller = GetComponent<CharacterController>();
playerInput = GetComponent<PlayerInput>();
cameraTransform = Camera.main.transform;
shootAction = playerInput.actions["Shoot"];
Cursor.lockState = CursorLockMode.Locked;
}
private void OnEnable()
{
shootAction.performed += _ => ShootGun();
}
private void OnDisable()
{
shootAction.performed -= _ => ShootGun();
}
private void ShootGun()
{
RaycastHit hit;
GameObject bullet = GameObject.Instantiate(bulletPrefab, barrelTransform.position, Quaternion.identity, bulletParent);
BulletController bulletController = bullet.GetComponent<BulletController>();
var tracer = Instantiate(tracerEffect, barrelTransform.position, Quaternion.identity);
tracer.AddPosition(barrelTransform.position);
if (Physics.Raycast(cameraTransform.position, cameraTransform.forward, out hit, Mathf.Infinity))
{
bulletController.target = hit.point;
bulletController.hit = true;
hitEffect.transform.position = hit.point;
hitEffect.transform.forward = hit.normal;
hitEffect.Emit(1);
muzzleFlash.Emit(1);
tracer.transform.position = hit.point;
}
else
{
bulletController.target = cameraTransform.position + cameraTransform.forward * bulletHitMissDistance;
bulletController.hit = false;
}
}
}
Bullet Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour
{
[SerializeField]
private GameObject bulletDecal;
private float speed = 500f;
private float timeToDestroy = 3f;
public Vector3 target { get; set; }
public bool hit { get; set; }
private void OnEnable()
{
Destroy(gameObject, timeToDestroy);
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (!hit && Vector3.Distance(transform.position, target) < .01f)
{
Destroy(gameObject);
}
}
private void OnCollisionEnter(Collision other)
{
ContactPoint contact = other.GetContact(0);
GameObject.Instantiate(bulletDecal, contact.point + contact.normal * 0.03f, Quaternion.LookRotation(contact.normal));
Destroy(gameObject);
}
}