Racast randomly shooting backwards!

Hey, I am trying to implement shooting with raycasts in my game but half oh the time I shoot the raycast goes backwards and hits the camera. here is the code below.

void Shooting()
    {

        muzzleFlashFX.Emit(1);
        tracerFX.SetPosition(0, muzzle.position);
        StartCoroutine(FlashTracer());

        currentAmmo--;

        // Creating acruacy offset
        Vector3 offset = UnityEngine.Random.insideUnitCircle * (1 - Mathf.Clamp(accuracy, 0, 1)) * accuracyOffset*0;

        RaycastHit hit;
        Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward + offset);
       
        if(Physics.Raycast(ray, out hit, shootRange))
        {
            Debug.Log(hit.transform.name);
            tracerFX.SetPosition(1, hit.point);

            Target target = hit.transform.GetComponent<Target>();

            if (target != null)
            { target.TakeDamage(damage);}

            if (hit.rigidbody != null)
            { hit.rigidbody.AddForce(-hit.normal * 60); }

            GameObject insImpact = Instantiate(impactFX, hit.point, Quaternion.LookRotation(hit.normal));
            Destroy(insImpact, 1f);
        }
        else
        {
           tracerFX.SetPosition(1, ray.origin + (ray.direction * shootRange));
        }
    }

What makes you think it is shooting backwards? Are you seeing the camera name print in line 18?

Do you have a collider on the camera? What happens if you set its layer to Layer “Ignore Raycast?” Does the issue go away?

I also note that on line 11 you have a final multiply-by-zero term so offset is always Vector3.zero right now.

This means your ray on line 14 is basically the camera position and camera forward, so that part seems reasonable.

I’m curious about Line 11:

Vector3 offset = UnityEngine.Random.insideUnitCircle * (1 - Mathf.Clamp(accuracy, 0, 1)) * accuracyOffset*0;

That’s always going to put (0, 0, 0) into offset. Is that intended?

Also curious about this part of Line 11:

(1 - Mathf.Clamp(accuracy, 0, 1))

Why subtract the clamped value from 1 here?

The tracker goes towards the player and yes, the debug.log shows it is hitting the player.

There is no collider on the camera and it shoots normal half the hit. changing the layer does nothing.

ya, I multiplied by zero to see if the random offset was somehow giving me the trouble but it is not.

for testing the shooting, i turned off the offset by multiplying it by zero it didn’t help.

clamping ensures the accuracy value being passed from the gun in between 0 - 100% accuracy . If the gun is 100% accurate then you will get 1-1=0 thus you will shoot straight.

Let me rephrase my statement:

What happens when you mark the thing you’re hitting as Ignore Raycast?

That takes us back to Kurt’s question, then. What makes you think the Raycast is hitting the camera? Raycast will only report hits for things with Colliders.

1 Like

oh, okay! setting my player to the ignore raycast fixed the issue! guess my camera was slightly inside my player to where half the time it would collider with the payer body. !

2 Likes

I’m still puzzled as to how something with no Collider collides with anything.

He was hitting his player apparently, not the no-collider camera.

Ah, I get it. Yes, that kind of thing can catch you by surprise.