so i am trying to code like an fps shooter and im doing the burst code to like rapid fire this is the code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public Camera playerCamera;
// Shooting
public bool isShooting, readyToShoot;
bool allowReset = true;
public float shootingDelay = 2f;
//Burst
public int bulletsPerBurst = 3;
public int burstBulletsLeft;
// Spread
public float spreadIntensity;
// Bullet
public GameObject bulletPrefab;
public Transform bulletSpawn;
public float bulletVelocity = 30;
public float bulletPrefabLifeTime = 3f; // seconds
public enum ShootingMode
{
Single,
Burst,
Auto
}
public ShootingMode currentShootingMode;
private void Awake()
{
readyToShoot = true;
burstBulletsLeft = bulletsPerBurst;
}
void Update()
{
if (currentShootingMode == ShootingMode.Auto)
{
// Holding Down Left Mouse Button
isShooting = Input.GetKey(KeyCode.Mouse0);
}
else if (currentShootingMode == ShootingMode.Single ||
currentShootingMode == ShootingMode.Burst)
{
// Clicking Left Mouse Button Once
isShooting = Input.GetKeyDown(KeyCode.Mouse0);
}
if (readyToShoot && isShooting)
{
burstBulletsLeft = bulletsPerBurst;
FireWeapon();
}
}
private void FireWeapon()
{
readyToShoot = false;
Vector3 shootingDirection = CalculateDirectionAndSpread().normalized;
// Instantiate the bullet
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, Quaternion.identity);
// Pointing the bullet to face the shooting direction
bullet.transform.forward = shootingDirection;
// Shoot the bullet
bullet.GetComponent<Rigidbody>().AddForce(shootingDirection * bulletVelocity, ForceMode.Impulse);
//Destory the bullet after some time
StartCoroutine(DestroyBulletAfterTime(bullet, bulletPrefabLifeTime));
// Checking if we are done shooting
if (allowReset)
{
Invoke("ResetShot", shootingDelay);
allowReset = false;
}
// Burst Mode
if (currentShootingMode == ShootingMode.Burst && burstBulletsLeft > 1) // we already shoot once before this check
{
burstBulletsLeft--;
Invoke("FireWeapon", shootingDelay);
}
private void ResetShot()
{
readyToShoot = true;
allowReset = true;
}
public Vector3 CalculateDirectionAndSpread()
{
// Shooting from the middle of the screen to check where we are pointing at
Ray ray = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
{
// Hitting something
targetPoint = hit.point;
}
else
{
// Shooting at the air
targetPoint = ray.GetPoint(100);
Vector3 direction = targetPoint - bulletSpawn.position;
float x = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
float y = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
// Returning the shooting direction and spread
return direction + new Vector3(x, y, 0);
}
}
}
private IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
{
yield return new WaitForSeconds(delay);
Destroy(bullet);
}
}
but it keeps saying the modifier public is not valid for this public Vector3 CalculateDirectionAndSpread() and the modifier private is not valid for this private void ResetShot()
anyone know?
Hi @Krispy8261
I think that you forgot to close brace } in the end of FireWeapon method, before from
private void ResetShot()
try to fix this and test, let me know that your issue was gone
No problem @Krispy8261 , you don’t need to apologize dude
You can try use the code bellow, I added a comment on lines that I made changes
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public Camera playerCamera;
// Shooting
public bool isShooting, readyToShoot;
bool allowReset = true;
public float shootingDelay = 2f;
//Burst
public int bulletsPerBurst = 3;
public int burstBulletsLeft;
// Spread
public float spreadIntensity;
// Bullet
public GameObject bulletPrefab;
public Transform bulletSpawn;
public float bulletVelocity = 30;
public float bulletPrefabLifeTime = 3f; // seconds
public enum ShootingMode
{
Single,
Burst,
Auto
}
public ShootingMode currentShootingMode;
private void Awake()
{
readyToShoot = true;
burstBulletsLeft = bulletsPerBurst;
}
void Update()
{
if (currentShootingMode == ShootingMode.Auto)
{
// Holding Down Left Mouse Button
isShooting = Input.GetKey(KeyCode.Mouse0);
}
else if (currentShootingMode == ShootingMode.Single ||
currentShootingMode == ShootingMode.Burst)
{
// Clicking Left Mouse Button Once
isShooting = Input.GetKeyDown(KeyCode.Mouse0);
}
if (readyToShoot && isShooting)
{
burstBulletsLeft = bulletsPerBurst;
FireWeapon();
}
}
private void FireWeapon()
{
readyToShoot = false;
Vector3 shootingDirection = CalculateDirectionAndSpread().normalized;
// Instantiate the bullet
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, Quaternion.identity);
// Pointing the bullet to face the shooting direction
bullet.transform.forward = shootingDirection;
// Shoot the bullet
bullet.GetComponent<Rigidbody>().AddForce(shootingDirection * bulletVelocity, ForceMode.Impulse);
//Destory the bullet after some time
StartCoroutine(DestroyBulletAfterTime(bullet, bulletPrefabLifeTime));
// Checking if we are done shooting
if (allowReset)
{
Invoke("ResetShot", shootingDelay);
allowReset = false;
}
// Burst Mode
if (currentShootingMode == ShootingMode.Burst && burstBulletsLeft > 1) // we already shoot once before this check
{
burstBulletsLeft--;
Invoke("FireWeapon", shootingDelay);
}
} // I added this close brace
private void ResetShot()
{
readyToShoot = true;
allowReset = true;
}
public Vector3 CalculateDirectionAndSpread()
{
// Shooting from the middle of the screen to check where we are pointing at
Ray ray = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
{
// Hitting something
targetPoint = hit.point;
return targetPoint; // I added this return
}
else
{
// Shooting at the air
targetPoint = ray.GetPoint(100);
Vector3 direction = targetPoint - bulletSpawn.position;
float x = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
float y = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
// Returning the shooting direction and spread
return direction + new Vector3(x, y, 0);
}
}
private IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
{
yield return new WaitForSeconds(delay);
Destroy(bullet);
}
}
I hope to help
Can you test if this code works for you?
public Vector3 CalculateDirectionAndSpread()
{
// Shooting from the middle of the screen to check where we are pointing at
Ray ray = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
Vector3 targetPoint;
Vector3 spread = Vector3.zero;
if (Physics.Raycast(ray, out hit))
{
// Hitting something
targetPoint = hit.point;
}
else
{
// Shooting at the air
targetPoint = ray.GetPoint(100);
float x = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
float y = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
// Returning the shooting direction and spread
spread = new Vector3(x, y, 0);
}
return (targetPoint - bulletSpawn.position).normalized + spread;
}
@douglasfsbarcelos what do you mean? im sorry im a beginner
i retyped the code just in case, the modifier public thing is still there i still have the private and 2 other things which is } expected and private modifier thing at the bottom for IEnumerator
thankyou so much you are a life saver
i know you helped me and im really sorry to ask you this but sometimes my bullets go to the left which was just the code that i made do you know how to fix that? like what i mean is it doesn’t go straight it like sometimes goes straight but not all the time most of the time it goes in a random direction
i get an error saying } expected
and then i add an extra } then the error is gone but then it says DestroyBulletAfterTime doesn’t exist in this context
im so sorry i am the dumbest kid alive i wasn’t putting it in the right spot it worked thankyou so much for your help im really sorry for bothering you today