- Home /
The question is answered, right answer was accepted
simple shooting script help
i am a noob with cs. i have a projectile script that shoots on click, i want to remove that function and add a function that makes it shoot every second. here is the script: using UnityEngine; using System.Collections;
public class ShootDemo : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 20;
void Update ()
{
if (Input.GetButtonDown("Fire1")) // this is the part i want to change
{
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
}
so what i need it for it to shoot WITHOUT CLICKING
any help would be great
thankyou
Answer by static_cast · Nov 25, 2014 at 04:08 PM
According to this, I modified it a little bit to fit your needs, so the code would be this:
using UnityEngine;
using System.Collections;
public class ShootDemo : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 20F;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
void Update() {
if (Time.time > nextFire) {
nextFire = Time.time + fireRate;
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
}
You forgot to write this:
using UnityEngine;
using System.Collections;
At the VERY top of the program, outside of the class. I updated my code so you can repaste it. ;)
EDIT: $$anonymous$$ake sure to hit that checkmark by my post so people will know that the question has been answered. ;)
Answer by etaxi341 · Nov 25, 2014 at 03:46 PM
This should work:
using UnityEngine;
using System.Collections;
public class ShootDemo : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 20;
float timeOver = 0f;
public float shootEvery = 1f;
void Update ()
{
if (timeOver < shootEvery)
timeOver += Time.deltaTime;
else{
timeOver -= shootEvery;
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
}
thanks but not what im after, i think its my fault that i didnt explain it right. i want to us this as an enemy projectile so i just want it to shoot without the need to click. ( and it had a few errors)
Then just remove the "Input.GetButton("Fire1") &&" in my Script :)
i get one error: "(1,26): error CS0246: The type or namespace name `$$anonymous$$onoBehaviour' could not be found. Are you missing a using directive or an assembly reference?"
You have to add this at the top:
using UnityEngine; using System.Collections;
But I saw you already got an working answer :) Just to complete my script thats what should be at the top :)
Answer by feyyd · Nov 25, 2014 at 03:57 PM
float fire_reshoot_track; //defined outside update loop that we use to store when weapon can be fired next
float interval; //defined outside update loop
//inside update loop
if ( fire_reshoot_track < Time.time )
{
Fire ();
fire_reshoot_track = Time.time + interval;
}
I don't know where I saw this method of tracking refire rates, but I prefer it to other methods I see.
so how would i implement this with my current script (i usually us js not cs)
Follow this Question
Related Questions
Hey can anyone help with a shooting script??? 0 Answers
Shootin Worminator Like Game 3 Answers
Shoot Script Help 1 Answer
click shooting how do i do it? 1 Answer
Firing System with two Shoot Points 1 Answer