system
November 14, 2010, 7:06pm
1
Hello,
I using the following script to launch my projectile on my game. When I click down, it fires on projectile. How can I make it so that when I hold down the mouse, the projectile continuous the fire (rapid fire, like a machine gun)?
Thank you.
var projectile : Rigidbody;
var speed = 20;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
}
}
all you need to do is:
instead of
if(Input.GetButtonDown("Fire1"))
you need to do
if(Input.GetButton("Fire1"))
that last one check every frame if you are holding down the Fire1 button (left mouse)
here's what you could try:
var projectile : Rigidbody;
var speed = 20;
var savedTime = 0;
function Update()
{
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot (seconds);
}
}
function Shoot(seconds)
{
if(Input.GetButton( "Fire1" ))
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
}
}
you could play with the values, like the var "oddeven"
or if that isn't what you want, just get rid of the var oddeven, and in the
if(oddeven)
{
Shoot (seconds);
}
change it to
if(seconds)
3dDude
January 3, 2011, 5:57pm
3
If unity3_users script is hard to get working correct you can look at this question:
http://answers.unity3d.com/questions/12593/first-person-shooter-help/12619#12619
I gave a script there, And I think it might be a little but easier to use.
look up Time.time in the documentation. It will give you this, a much simpler solution than the other one.
var projectile : GameObject;
var fireRate : float;
private var lastFire : float = 0;
function Update()
{
if(Input.GetButton("Fire1") && Time.time > lastFire)
{
lastFire = Time.time + fireRate;
Instantiate(projectile, transform.position, quaternion.identity);
}
}
ok, so here is my solution for shot timing, muzzle flash timing, and audio timing.
This script is in C#, hope you know that languageā¦
using UnityEngine;
using System.Collections;
public class gun : MonoBehaviour {
//Author : Electric Fountain Studios
//Variables
public float bulletsLeft;
public float bulletImpulse = 100.0f;
public float reload;
public float bulletCount = 30;
public float reloadTimer = 0.1f;
public float ShotTimer = 0.01f;
public float bullet = 1.0f;
public float timeToReloadPoint = 0.0f;
public float audioVolume = 5.0f;
public float nextFire = 0.0f;
//Game Objects
public GameObject gun_prefab;
public GameObject muzzle_flash;
public GameObject bullet_prefab;
//Audio
public AudioClip shootSound;
float theBullet;
void Start () {
bulletCount = bulletsLeft;
}
void Update () {
if( Input.GetButton("Fire1") && Time.time > nextFire) {
//Time
nextFire = Time.time + ShotTimer;
//Shooting Action
GameObject theBullet = (GameObject)Instantiate(bullet_prefab, gun_prefab.transform.position, gun_prefab.transform.rotation);
theBullet.rigidbody.AddForce( gun_prefab.transform.forward * bulletImpulse, ForceMode.Impulse);
//M4 audio
audio.PlayOneShot(shootSound , audioVolume );
//M4 Muzzle Flash
Instantiate(muzzle_flash, gun_prefab.transform.position, gun_prefab.transform.rotation);
}
if( Input.GetButtonDown("Fire1") ) {
bulletsLeft = bulletCount - bullet;
}
if(bulletsLeft != timeToReloadPoint ){
print("Need To Reload");
}
}
}