- Home /
c# weapon shooting script using raycasting help
So I'm creating this script to make my gun fire, so far I have it reloading exactly how I want it to I would just like to make the weapon shoot. I looked up a few tutorials and found that most are using java script so I didn't really learn much and I ended up here http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html which I read but I wasn't sure how to implement this into my script to make the gun fire I was hoping to make it similar to this video https://www.youtube.com/watch?v=mpxim8YbsMk&feature=youtu.be except using c#. and yes since I got my post blocked earlier, there are many ways of making a bullet shoot. I'm just looking for one of the ways.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public bool outofammo;
public int bullets = 7;
public GameObject deagle; //my gun
public float time = 1;
public float firerate = 1;
public bool fired = false;
public bool Cannotfire = false;
public bool timedown;
void Update ()
{
if (Input.GetKeyDown(KeyCode.R) && bullets < 7)
{
deagle.animation.Play ("Reload");
bullets = 7;
}
if (Input.GetButtonDown("Fire1"))
{
timedown = true;
if (bullets == 0)
{
bullets = 7;
outofammo = true;
timedown = false;
}
else
outofammo = false;
if (outofammo == true)
{
deagle.animation.Play ("Reload");
}
}
if (timedown == true)
{
time = (time - (firerate *Time.deltaTime));
if (time <= 0)
{
bullets = bullets -1;
fired = true;
timedown = false;
time = 1f;
fired = false;
}
}
if (fired == true)
time = 1;
}
}
On the script reference page, right at the top should be an option to change the code from javascript to C#. It's located in the upper left.
Answer by TykoX64 · Mar 14, 2014 at 02:51 PM
Basically you would create a ray starting from your gun and pointing the direction you're firing and test it with Physics.raycast. you also need a HitInfo variable so that you can test the collision. The last option on the script reference shows this very well. http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
instead of using ScreenPointToRay() you can create a new Ray(gun.position, gun.fireDirection).
Okay so I edited my script and now I have an error similar to this one
http://answers.unity3d.com/questions/27678/nullreferenceexception-object-reference-not-set-to.html
my error being: NullReferenceException: Object reference not set to an instance of an object NewBehaviourScript.Update () (at Assets/Scripts/NewBehaviourScript.cs:34)
I can see how to fix it from that link but I'm not sure how I would apply the same technique to my script
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour
{
public bool outofammo;
public int bullets = 7;
public GameObject deagle; //this is my gun
public float time = 1;
public float firerate = 1;
public bool fired = false;
public bool Cannotfire = false;
public bool timedown;
Transform Effect;
float Damage = 100;
// object particleClone;
RaycastHit hit;
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R) && bullets < 7)
deagle.animation.Play ("Reload");
}
if (Input.GetButtonDown("Fire1"))
{
timedown = true;
Vector3 forward = transform.TransformDirection (Vector3.forward);
hit.transform.Send$$anonymous$$essage ("ApplyDamage", Damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
if (bullets == 0)
{
bullets = 7; to being 7
outofammo = true;
timedown = false;
}
else
outofammo = false;
if (outofammo == true)
{
deagle.animation.Play ("Reload");
}
}
if (timedown == true)
{
time = (time - (firerate *Time.deltaTime));
if (time <= 0)
{
bullets = bullets -1;
fired = true;
timedown = false;
time = 1f;
fired = false;
}
}
if (fired == true)
time = 1;
}
}
I converted your answer to a comment, did you mean to comment on Tyko answer ?
I think there are missing lines in your last code copy/paste. At line 34, there is only the bullets variable, so this one cannot be null...
Huh what do you mean? I wrote everything myself apart from the raycasting part, lines 14-17, 28 and 32. Do you mean my bullets can't be equal to 0? because I created a fire-rate by using
time = (time - (firerate *Time.deltaTime));
if (time <= 0)
this is where my bullets are taken away so if my bullets = 0 it will automatically reload and fill the clip back to 7. Is that what you mean?
Your answer
Follow this Question
Related Questions
How can I shooting script c#? 0 Answers
Make a Gun fire multiple times 3 Answers
Ammo Script 1 Answer
Best way to make a gun? 1 Answer
How do shoot a bullet when the right button is clicked? 1 Answer