Question by
grdomenico · Nov 12, 2018 at 04:42 PM ·
fpsshootingshootautomaticautomatically
Can i make automatic fire?
I have this script for movement ad for shoot. I want to shoot automatically when i'm clicking the lest mouse button.Please help (sorry for the bad english).
Script:
using UnityEngine; using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour { //GetComponent().enabled = false; //GetComponent().enabled = false; public GameObject bulletPrefab; public GameObject bullet2Prefab; public Transform bulletSpawn;
void Update()
{
if (!isLocalPlayer)
{
return;
}
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
if (Input.GetKeyDown(KeyCode.Mouse0))
{
CmdFire();
AudioSource shoot = GetComponent<AudioSource>();
shoot.Play();
}
if (Input.GetKeyDown(KeyCode.Mouse1))
{
CmdFire2();
AudioSource shoot = GetComponent<AudioSource>();
shoot.Play();
}
}
[Command]
void CmdFire2()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bullet2Prefab,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 20;
// Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
// This [Command] code is called on the Client …
// … but it is run on the Server!
[Command]
void CmdFire()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 10;
// Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 1.5f);
}
public override void OnStartLocalPlayer()
{
}
}
Comment
Your answer
Follow this Question
Related Questions
how to add shoot cooldown 1 Answer
How do I define the direction and speed of a projectile separately? (JS) 0 Answers
Held UI button C# 0 Answers
On colision Enter 1 Answer