Hello all. I’m making a third-person shooter game, and I’m using Physics.Raycast to act as a hitscan for my guns. However, when I added a laser sight to my gun, I noticed that some shots weren’t aligned with the laser (debris prefab was spawning a little below where it should). After logging the hit point of my raycast, I saw that it was the Raycast function that was returning different points even though the position and direction of the ray never changed. This could potentially be a big problem, as my game is about stealth and precision.
Here is my code:
public float FireRate = 0.1f;
public float RechamberTime = 0.1f;
public float Damage = 25.0f;
float CurrentClip;
float ClipSelected = 1;
public float Clip1 = 30f;
public float Clip2 = 30f;
public float Clip3 = 30f;
public float Range = 100;
public GameObject CameraPosition;
public AudioClip Shot;
RaycastHit HitInfo;
if (RechamberTime <= 0 && CurrentClip > 0 && Input.GetMouseButton (0)) {
RechamberTime = FireRate;
audio.PlayOneShot(Shot);
if (ClipSelected == 1) {
Clip1 -= 1;
}
if (ClipSelected == 2) {
Clip2 -= 1;
}
if (ClipSelected == 3) {
Clip3 -= 1;
}
Debug.Log ("Bullets Left: " + CurrentClip);
//Hitscan by casting Ray
Ray Aim = new Ray (this.gameObject.transform.position, this.gameObject.transform.forward);
if (Physics.Raycast (Aim, out HitInfo, Range)) {
Vector3 HitPoint = HitInfo.point;
GameObject Target = HitInfo.collider.gameObject;
Debug.Log ("HitObject: " + Target.name);
Debug.Log ("Hit Point: " + HitPoint);
Health TargetHasHP = Target.GetComponent<Health> ();
if (TargetHasHP != null) {
TargetHasHP.TakeDamage (Damage);
}
//Spawn prefab
Shootable DebrisAttatched = Target.GetComponent<Shootable> ();
if (DebrisAttatched != null) {
DebrisAttatched.SpawnPrefab (HitPoint);
}
}
Sorry for the long block of code, but I’ve scoured it over and over and can’t find anything wrong. I followed a tutorial by quill18creates on YouTube to the letter, so you can imagine my frustration. ^^;
Don’t think it’s the script. The key line is exactly perfect, Line 33: Ray Aim = new Ray (transform.position, transform.forward);
(this.gameObject is redundant.)
That fires directly from whatever the script is on, directly the way it’s aiming. If the script is on the +Z business end of the gun, this is exactly where the little blue arrow on the gun is aimed.
Something in the scene is set up wrong. Maybe the gun is centered too low (so the blue line comes out below the barrel.) Maybe the laser is aimed funny. Or maybe aim is fine but the debris prefab drops too quickly. Or this script is on the arm, and not the slightly angled gun… . I’d make a tiny red cube, no collider, and put testCube.position = HitPoint
after line 35. Should show the problem better.
The laser sight is a line renderer component attached to the gun. It draws a line originating from the barrel of the gun and travels 1000 units forward.
I’m pretty sure the raycast ISN’T the problem. I followed a YouTube tutorial to the letter in order to set it up.
As far as testing it on another object, I placed two cubes on top of each other and gave them both colliders, and it did the same thing. I don’t see how anything else could be moving or rotating, considering that there’s no references to anything other than the Sparks prefab and the gun itself, and neither of those references says anything about moving or rotating…
I’ll triple check the tutorial and see if I did anything wrong as far a setting up the scene, because I’m almost 100% sure there’s nothing wrong with the script…
***************************** EDIT *****************************************
Okay, so I’ve discovered the problem and fixed it. I had a RigidBody component attatched to my Player object, and it had gravity checked. I disabled gravity and it works. I’m not quite sure why this would be causing a problem because my player object ALSO had a character controller attached (which comes standard with a capsule collider),and if gravity was causing the gun to move, and gravity is a downward force, and the gun was attached to the object with a collider that was supposed to STOP gravity from acting on the object, then why would gravity be pulling things down that were already on the ground? Anyways, thanks for your advice, guys. I appreciate you all taking the time to help.