- Home /
My shooting script sometimes shoots down
Firstly, ty for taking the time to read this. I'll leave my shooting script below so you can check it, it's really short so I hope it's not something bothering.
What happens in my case is that, sometimes (probably 1/5 bullets or so) hit below the point I am aiming at, the weird thing about this behaviour is that every time a bullet does this, it goes farther below than the last one, so I end up with correct, working bullets and with bullets that go under the aim point increasingly. (If you need a video to understand better this behaviour feel free to ask for one, I'll upload it to Youtube and link it here).
As I said, here's the script:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class gunScript : MonoBehaviour
 {
     public float damage = 10f;
     public float range = 100f;
     public float fireRate = 15f;
     public float impactForce = 30f;
 
     public Camera fpsCam;
     public ParticleSystem muzzleFlash;
     public GameObject impactEffect;
 
 
     private float nextTimeToFire = 0f;
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire && Cursor.lockState == CursorLockMode.Locked)
         {
             nextTimeToFire = Time.time + 1f / fireRate;
             Shoot();
         }
     }
 
     void Shoot()
     {
         if(muzzleFlash != null)
         {
             muzzleFlash.Play();
         }
         
         RaycastHit hit;
         if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
         {
             Debug.Log(hit.transform.name);
 
             Target target = hit.transform.GetComponent<Target>();
             if(target != null)
             {
                 target.TakeDamage(damage);
             }
 
             if(hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * impactForce);
             }
 
             if(impactEffect != null)
             {
                 GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
                 Destroy(impactGO, 2f);
             }
             
         }
 
     }
 }
 
Answer by NikkF · Mar 13 at 07:13 PM
Your script looks fine, it might be a problem with your camera itself. Perhaps a camera movement script is causing it? A video would be really helpful
Your answer
 
 
             Follow this Question
Related Questions
Random Raycast inside crosshairs 1 Answer
Arrow not shooting the in right orientation 0 Answers
Problem in Shooting Accuracy 0 Answers
Shotgun raycast 1 Answer
Why do I sometimes hit my Collider and sometimes not 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                