- Home /
How can I make my gun's bullets go in random bullets(Like how do I Make them inaccurate randomly) And How do I also but a fire rate on it
So Im making a 3d top down shooter game and I was able to do shooting but I dont know how I can make bullets inaccurate and put fire rates on them using System.Collections; using System.Collections.Generic; using UnityEngine; public class ProjectileController : MonoBehaviour { //Variables public float Range = 50f; public float Speed; private GameObject TriggerEnemy; public float Damage; public float Accuracy; void Start() { } // Update is called once per frame void Update() { //Bullet Speed and direction transform.Translate(Vector3.forward * Time.deltaTime * Speed); //Bullet Range if(transform.position.z < -Range) { Destroy(gameObject); } else if(transform.position.z > Range) { Destroy(gameObject); } else if(transform.position.x < -Range) { Destroy(gameObject); } else if(transform.position.x > Range) { Destroy(gameObject); } } private void OnTriggerEnter(Collider other) { if(other.tag == "Zombie") { TriggerEnemy = other.gameObject; TriggerEnemy.GetComponent<ZombieAiScript>().ZombieHealth -= Damage; Destroy(gameObject); } } }
And`using UnityEngine.SceneManagement; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { //Variables public float PlayerLife = 5f; private bool gameOver = false; public GameObject PistolBulletSpawner; public float FireRate; public GameObject PistolProjectile; public float speed = 10f; public float turnSpeed = 50f;
public GameObject PlayerModel;
private float horizontalInput;
private float verticalInput;
// Start is called before the first frame update
void Start()
{
gameOver = false;
}
void Update()
{
//Shoot
if(Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
if(gameOver == false)
{
//Move Up and Down
verticalInput = Input.GetAxis("Vertical");
PlayerModel.transform.Translate(new Vector3(0,0,1) * verticalInput * Time.deltaTime * speed);
//Rotate Left when left arrow key is pressed
if(Input.GetKey("left"))
{
PlayerModel.transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
}
//Rotate Right when right arrow key is pressed
if(Input.GetKey("right"))
{
PlayerModel.transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
}
//If Player Has no life then GameOver is set to true
if(PlayerLife == 0)
{
GameOver();
}
//If the player has died/gameover is set to true and have pressed the R key the game will reload the level again
if(gameOver == true && Input.GetKeyDown(KeyCode.R))
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
}
void GameOver()
{
gameOver = true;
Debug.Log("You Die");
}
void Shoot()
{
Instantiate(PistolProjectile.transform, PistolBulletSpawner.transform.position, PistolBulletSpawner.transform.rotation);
}
}` Thank You!
Answer by MegaBomb_Unity · Oct 09, 2021 at 01:01 PM
Warning, I am not the most qualified person to answer questions. But here is some advice.
For fire rate, have you ever heard of IEnumerator? Basically, it is kinda like a void that allows you to do actions across multiple frames. First create a boolean to detect fire rate. use an IEnumerator to temporarily disables the boolean and code it so that you cant shoot while the boolean is false.
public GameObject PlayerModel;
private float horizontalInput;
private float verticalInput;
bool allowFire = true;
// Start is called before the first frame update
void Start()
{
gameOver = false;
}
void Update()
{
//Shoot
if (Input.GetKeyDown(KeyCode.Space))
{
if (allowFire)
{
Shoot();
StartCoroutine(ActivateFireRate());
}
}
if (gameOver == false)
{
//Move Up and Down
verticalInput = Input.GetAxis("Vertical");
PlayerModel.transform.Translate(new Vector3(0, 0, 1) * verticalInput * Time.deltaTime * speed);
//Rotate Left when left arrow key is pressed
if (Input.GetKey("left"))
{
PlayerModel.transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
}
//Rotate Right when right arrow key is pressed
if (Input.GetKey("right"))
{
PlayerModel.transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
}
//If Player Has no life then GameOver is set to true
if (PlayerLife == 0)
{
GameOver();
}
//If the player has died/gameover is set to true and have pressed the R key the game will reload the level again
if (gameOver == true && Input.GetKeyDown(KeyCode.R))
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
}
void GameOver()
{
gameOver = true;
Debug.Log("You Die");
}
void Shoot()
{
Instantiate(PistolProjectile.transform, PistolBulletSpawner.transform.position, PistolBulletSpawner.transform.rotation);
}
IEnumerator ActivateFireRate()
{
allowFire = false;
yield return new WaitForSeconds(FireRate);
allowFire = true;
}
For random bullet spread, I don't know the specifics because I am not experienced enough. However, what you want to do is call in
float randomX = Random.Range(-Accuracy, Accuracy);
float randomY = Random.Range(-Accuracy, Accuracy);
This creates a random number generator. Once you do that, you need a way to add that to the x and y values of Vector3.forward. This is as much as I can offer. You're gonna need to ask someone else to help you with that.
Your answer
Follow this Question
Related Questions
Unity Web Player does not respond to mouse clicks 1 Answer
,Look rotation viewing vector is zero and character always facing 1 direction when idle 0 Answers
Allow player to import 3D model for character 0 Answers
How do I make my player's stamina stop decreasing at 0? 1 Answer
Enemy Will not die 1 Answer