Help! I was watching a tutorial on how to make a working gun.
My Error Message(s) is/are:
1. Assets\Scripts\GunScript.cs(40,13): error CS0103: The name ‘allowButtonHold’ does not exist in the current context
2. Assets\Scripts\GunScript.cs(41,25): error CS0119: ‘GunScript.MyInput()’ is a method, which is not valid in the given context
3. Assets\Scripts\GunScript.cs(64,97): error CS0103: The name ‘rayHit’ does not exist in the current context
4. Assets\Scripts\GunScript.cs(66,23): error CS0103: The name ‘rayHit’ does not exist in the current context
Sorry if my code is too long/confusing. Please give me any tips you have for this, tysm.
Also, I left (Line 68 to 71) in comments because I haven’t made the script yet.
My Code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GunScript : MonoBehaviour
{
//Gun stats
public int damage;
public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
int bulletsLeft, bulletsShot;
//Bools
bool shooting, readyToShoot, reloading;
//Reference
public Camera fpsCam;
public Transform attackPoint;
public RaycastHit raycastHit;
public LayerMask whatIsEnemy;
public Text text;
private void Awake()
{
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
MyInput();
//SetText
text.text = bulletsLeft + " / " + magazineSize;
}
private void MyInput()
{
if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
else shooting = MyInput.GetKeyDown(KeyCode.Mouse0);
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
//Shoot
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
Shoot();
}
}
private void Shoot()
{
readyToShoot = false;
//Spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
//Calculate Direction with Spread
Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);
//Raycast
if (Physics.Raycast(fpsCam.transform.position, direction, fpsCam.transform.forward, out rayHit, range, whatIsEnemy))
{
Debug.Log(rayHit.collider.name);
/*if (rayHit.collider.CompareTag("Enemy"))
rayHit.collider.GetComponent<ShootingAi>().TakeDamage(damage);*/
//of course your Enemy needs to be tagged as "Enemy" and needs to have a script with the TakeDamage function :smile:
}
bulletsLeft --;
Invoke("ResetShot", timeBetweenShooting);
if (bulletsLeft > 0 && bulletsLeft > 0)
{
bulletsShot = bulletsPerTap;
Shoot();
}
Invoke("Shoot", timeBetweenShots);
}
private void ResetShot()
{
readyToShoot = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime);
}
private void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
}