- Home /
Using RayCast to do Continuous Shooting?
Hi, I currently have a RayCast that acts more of a combat rifle/pistol. It shoots once per click. How do I transform my script to make it into a machine gun script where I don't have to click once, but instead hold the button for continuous fire?
public class Shooter : MonoBehaviour
{
void Update ( )
{
// 1. Wait for a mouse click.
if ( Input.GetButtonDown( "Fire1" ) )
{
Shoot( );
}
}
void Shoot ( )
{
// 2. Create a ray that travels from your camera
// in the direction it's facing.
Ray ray = new Ray( transform.position, transform.forward );
RaycastHit hit;
if ( Physics.Raycast( ray, out hit ) )
{
// 3. Check the first thing it hits, is it your enemy?
// If so (actually there is nothing defined as enemy);
hit.transform.SendMessage( "OnBullet",
SendMessageOptions.DontRequireReceiver );
}
}
Answer by Lovrenc · Dec 31, 2012 at 05:54 PM
Use this event instead of GetButtonDown. Only this way you will fire on every update so you need to implement "RateOfFire" variable and use Time.deltaTime to slow down your shooting.
LAZINESS EDIT:
Dude, you have 14 questions without single acception. And really simple instruction here but you dont event bother. There was no "small problems" to fix. Search and replace would almost do it....
I didnt compile this:
public class Shooter : MonoBehaviour
public float timeBetweenShots = .3f;
private float timeSinceLastShot = 0;
{
void Update ( )
{
// 1. Wait for a mouse click.
timeSinceLastShot += Time.deltaTime;
if ( Input.GetButton( "Fire1" ) && timeSinceLastShot >= timeBetweenShots)
{
Shoot( );
}
}
void Shoot ( )
{
// 2. Create a ray that travels from your camera
// in the direction it's facing.
Ray ray = new Ray( transform.position, transform.forward );
RaycastHit hit;
if ( Physics.Raycast( ray, out hit ) )
{
// 3. Check the first thing it hits, is it your enemy?
// If so (actually there is nothing defined as enemy);
hit.transform.SendMessage( "OnBullet",
SendMessageOptions.DontRequireReceiver );
timeSinceLastShot = 0;
}
}
This is slightly irrelevent because im using a raycast! Could you be more specific and maybe adjust my script so I can use it?
That is not irrelevant at all. Read the documentation. The only difference is that the example uses projectile logic, while yours uses Raycast. It is really easy to adapt the code from the example to yours.
Everyone here wants things DONE for them. I mean you are here to LEARN not to get job done for you. Especially if its such an easy adaptation. You have to change event and add a little logic to have some constant rate of fire ins$$anonymous$$d of damn $$anonymous$$igun.
Not to mention i didnt even notice there is your EXACT PROBLE$$anonymous$$ in the event documentation.
EDIT: Looking at that you never accepted ANY answer here makes me wonder why we even try sometimes.
First of all, I tried adjusting my own script, but it didn't work! So that's why I asked for a complete script. I am one that learns more from a completed script rather than fixing small problems along the way. I only do that when I can, I can't fix a problem I have no clue about. There is no need to be rude, if you can't help, then leave.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Unity c# Raycast axis stuck on one axis 0 Answers
not losing ammo when shooting 1 Answer
Firing a RayCast in a Raycast, Rayception 1 Answer
Problem Standing into Objects 0 Answers