Volix
1
HELLO!
i am making a first person shooter.
but i have a problem.
my character shoot every frame. i don’t know how to fix this so if any one has a fix please write thanks in adavance!
BTW here’s my coding
#pragma strict
var BulletPrefab:Transform;
private var dead = false;
function OnTriggerEnter(hit : Collider)
{
if (hit.gameObject.tag == "Bullet")
{
Application.LoadLevel(2);
}
}
function Update ()
{
if (Input.GetKey(KeyCode.Mouse0))
{
var Bullet = Instantiate(BulletPrefab, gameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * 2250);
}
if (Input.GetKey(KeyCode.Escape))
{
Application.LoadLevel(0);
}
}
_RC
2
I do such things with a bool.
var isShooting = false;
var fireRate = 0.1;
function Update ()
{
if (Input.GetKey(KeyCode.Mouse0))
{
if(!isShooting){ Shoot(); }
}
}
function Shoot(){
isShooting = true;
var Bullet = Instantiate(BulletPrefab, gameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * 2250);
yield WaitForSeconds(fireRate);
isShooting = false
}
Here you go:
var BulletPrefab : GameObject;
var Cooler : float = 1;
var Timer : float = 0;
function OnTriggerEnter(hit : Collider)
{
if (hit.gameObject.tag == "Bullet")
{
Application.LoadLevel(2);
}
}
function Update ()
{
if(Timer == 0)
{
if (Input.GetKey(KeyCode.Mouse0))
{
var Bullet = Instantiate(BulletPrefab, gameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * 2250);
Timer = Cooler;
}
}
if(Timer > 0)
{
Timer -= Time.deltaTime;
}
if(Timer < 0)
{
Timer = 0;
}
if (Input.GetKey(KeyCode.Escape))
{
Application.LoadLevel(0);
}
}
And I definalty recomend you watch some tutorials, just as Lockstep said.
Here’s one I think is very good for FPS: http://www.youtube.com/user/narutoisgreat1234?feature=g-user-u