How to make gun shoot in the crosshair & automatic fire (Javascript)
Hello guys, So im new in unity and coding and i want a bit of help. I have a script that i found in a tutorial in youtube where i can shoot a single bullet and the bullet will go where the weapon is pointing. The problem is that i want it to go perfectly in the center of my screen, where i have made my crosshair. Also how can i create the automatic fire mode? I mean that if I simply click, i will shoot 1 bullet whereas if i hold down the left mouse button it will continue shooting until i hit the ammo limmit. I would love to be able if you could add a variable where it will have a delay between each bullet, that i can change between the weapons.
I forgot to mention earlier that i don't have a bullet "Object". Also the script is on the gun itself and i have a separate script where i basically say, where i press "Fire1" it would play an animation and a sound. I don't know if this will help but i thought it may have to do with something...
You can take a look in the script below. Thanks anyways :)
var DamageAmount : int = 32;
var TargetDistance : float;
var AllowedRange : float = 100;
function Update () {
if(Input.GetButtonDown("Fire1")) {
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange) {
Shot.transform.SendMessage("DeductPoints", DamageAmount);
}
}
}
}
Please do not flame. I have absolutely no idea of coding. I am more in the 3d workspace than in the coding stuff which i really want to get into :)
If you're worried, the HelpRoom area is fine for Qs like this (I moved it there.) There's also no wait for a moderator to approve it.
But you should be able to look those up separately - shooting delay and various bullet raycasts. Lots of old Qs about that here.
Answer by UrielKane · Jun 30, 2016 at 04:36 AM
Well the fire rate task is almost really simple and it's a metter of one line of code and one sentence if(). This can be done in many ways like almost everything in coding. But one way wich is what i usually use is the Time class. I.E.
public var FireRate : float = 0.1f;
private var FireTimer : float;
function Update () {
if(Time.time > FireTimer && Input.GetButton("Fire1")){
StartCoroutine(FireOneShot());
FireTimer = Time.time + FireRate;
//this line of code can be put on the FireOneShot coroutine to.
}
}
function FireOneShot () {
//Do the shot logic.
}
The only problem with this solution is that sometimes you need to run it on FixedUpdate in order to have frame independency in the firerate. If you want frame independecy don't forget to use Time.deltaTime insted of Time.time.
On the other hand the logic for making the shot to be cast exactly through the crosshair. Depend on what exactly the circunstances and your needs about that. Becouse although the simple solution is to cast the ray using something like.
function Update() {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, 100)) {
print ("Hit something");
}
}
Sometimes you will need something more complex for example. Casting a ray from the gun position when is shot a lasser or something like that. In those cases you will need to cast two rays or use linecast instead, in order to check where the ray should be directed. I am currently just developing this system I just mentioned. I'm using a ray to check if in the point of the shoot is hitting something. The i change the direction of the second ray based on the hit info and so on. Then finally i have change completely the projectile behaviour for me rockets and else. Using linecast in one hand and on the other using triggers instead of clasic colisions.
I hope this will be helpfull. @PaulDabest
Your answer
Follow this Question
Related Questions
How to make an automatic fire mode for a gun (Javascript) 0 Answers
My gun is only shooting bullets every 8th shot..? 0 Answers
Creating Random Target Points on Game Object for Player to Shoot 0 Answers
Help With Recoil Script 0 Answers
how can i set the direction of my gun to the Raycast collision point? 1 Answer