Hello, I need help with my script. Yes, I know this has been asked loads of times but I didn’t get anything at all from those scripts and answers. Can some please tell me how I can achieve this and also give a detail explanation? Also I need to know how I can get the BulletSpawner which is an image in a canvas in the middle of the screen to match the guns recoil? Thanks. ![]()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerGunScript : MonoBehaviour
{
public Transform BulletSpawn;
public AudioSource M4A1_SHOT;
public Camera playerCam;
public float RandomX;
public float RandomY;
public Animator crsshairAnim;
void Start()
{
maxAmmo = 40;
ADS_Script = GetComponent<PlayerADSScript>();
}
void FixedUpdate()
{
RaycastHit hit;
if (Input.GetMouseButton(0) && !(magAmmo <= 0) && canShoot)
{
//Addes spread to bullets
BulletSpawn.localPosition = Spread;
if (((BulletSpawn.localPosition.x > 40 || BulletSpawn.localPosition.x < -40) || BulletSpawn.localPosition.y > 40 || BulletSpawn.localPosition.y < -40) && crsshairAnim.GetBool("PlayerIdle"))
{
BulletSpawn.localPosition = MIDDLE;
}
if (Physics.Raycast(BulletSpawn.position, BulletSpawn.forward, out hit))
{
if (hit.collider.gameObject.tag == "Enemy")
{
Debug.Log("Hit!");
Destroy(hit.collider.gameObject);
}
}
}
}
Plus some things are removed from the code so this is not the full code but I think it has enough to have recoil added.