- Home /
Not technical or specific.
How can I shooting script c#?
Hey, I'm new to programming and I created a script to make my gun reload and add bullets maybe not the best way of doing this but it's how I decided to do it from my head without tutorials or anything. Anyway what I wanted to do was actually make my gun shoot, the bullets go down and all it just doesn't fire or anything. I have never used raycasting so I'd like to implement this into my script and was hoping someone could help me with that. Also sorry, I didn't do commenting on my script to show what's going on D: hopefully you people can just figure that out... also I am using c#
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public bool outofammo;
public int bullets = 7;
public GameObject deagle;
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;
}
}
To find how raycasts work look at the documentation. Otherwise this question is not specific enough to answer. There are many (infinite?) ways of "shooting" a "bullet".
I was looking for a way to implement the raycasting for the bullet, I don't $$anonymous$$d how it's done I've looked at the documents saying how to do it http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html but I'm asking how I would implement it into this script that I've created. I also have looked up tutorials, which usually just end up being in javascript which I would prefer not to use. Will I have to recreate this post if I would like to actually have people help me?
There are many ways of "shooting" a "bullet" as I said "I have never used raycasting so I'd like to implement this into my script and was hoping someone could help me with that." I'd like to use raycasting obviously. yes, there are many ways to make a bullet shoot, I was just looking for one of those ways. People do look at the documentation it just doesn't help for example
// Raycast up to 100 meters down
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
}
this is one of the ways it recommends to do it, however this does not tell me how I would put this in my script. I may not have been technical enough but you really don't understand the situation without reading through the post properly. $$anonymous$$aybe I didn't mention some of this, but you could post a question ins$$anonymous$$d? maybe discuss how I wanted to make it work even? if I was new to this and had no idea what raycasting was and I wanted to make a bullet shoot you would help them by providing details about how you can do it would you not?
I am not trying to be smart or anything but it seems to me like you just deny people's posts for simple reasons such as this. So please, quit blocking peoples posts for simple reasons that could be solved in a simple answer. I am not here to argue, I'm here for help from other people. Thank you
There's a certain point in your script where you want the "bullet" to be "fired", since it's your script, you should know where that point is. At that point "implement" whatever kind of "firing" of "bullets" you want.
Also the difference between C# and UnityScript is no $$anonymous$$ute, it's really easy to follow tutorials or other pieces of code in either language (even Boo is pretty easy to understand).
@Crumpet The issue with your question is that you never actually tried it yourself. Go and try it as best you can, using the documentation as a guide. If you get stuck on SPECIFICS, then ask about those. Your question and others like it are essentially saying "Please write some code for me because I don't know how".
If you tried, and got errors, then post the code you tried, post the errors you got, and we will be very happy to help with that.
Follow this Question
Related Questions
c# weapon shooting script using raycasting help 1 Answer
Best way to make a gun? 1 Answer
How do shoot a bullet when the right button is clicked? 1 Answer
Help on a pick up/reload script 3 Answers