-2

I'm trying to shoot a raycast from a gameobject at the front of a gun that is being carried by an XR hand. The gun shoots fine most of the time, but sometimes, for some reasons beyond my fragile little minds comprehension, it shoots in the wrong direction. It shoots in random directions, but never more than 90 degrees of where it should be. This is my code. [I have attached a video of it happening.][1] Every time I think I find a pattern, it changes. All I can say is that I have reason to believe it's related to mesh colliders. Only very vague reason though. Edit: https://youtu.be/JxC4wq9nVRw here's an updated video, it shows the problem more clearly. I also changed the redundant if statements.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GunBasic : MonoBehaviour
{   
    public Transform BulletShootPoint;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;
    public AudioSource audioSource;
    public AudioClip audioClip;
    public float Ammo;
    public  TextMeshPro text;

    public void Fire()
    {if (Ammo > 0)
        {
            muzzleFlash.Play();
            audioSource.PlayOneShot(audioClip);


            RaycastHit hit;
            if (Physics.Raycast(BulletShootPoint.transform.position, BulletShootPoint.transform.forward, out hit) && Ammo > 0)
            {
                Debug.Log(hit.transform.name);

                GameObject impactDestroy = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));

                Destroy(impactDestroy, 2f);

            }
            Ammo -= 1f;


        }
    }
    private void Update()
    {
        text.text = Ammo.ToString();
  

    }
    public void Reload()
    {
        Ammo = 10;
    }
    public void OnTriggerEnter(Collider other)
    {
       if(other.tag == "ReloadTag")
        {

            Ammo = 10f;
        }
    }


}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • I'm a bit unsure what exactly I'm supposed to see in the video ^^ and just in general why are you checking `Ammo > 0` three times? – derHugo Jun 09 '21 at 06:38
  • @derHugo Thanks for the comment, in the video you can see the raycast shooting in the wrong direction from the gun. It moves from a location outside the door to right in front of the gun where it is supposed to be. The whole time the gun doesn’t move, at least not enough to justify such a dramatic change in the raycast. It might be hard to see but the impact effect that’s in the wrong place is just to the left of the door outside. Probably should have made the directional light brighter. Also, I’m checking Ammo three times because I’m an idiot. I’ll fix that, thanks for pointing it out. – Immortal Hamster Jun 09 '21 at 09:06
  • Most likely something else is moving/rotating your BulletShootPoint, this script is not transforming it anywhere. Btw @derHugo is right, you don't need to check for ammo all the time, you can remove the checks and just add as first line of `Fire()` method to be `if (ammo < 1) return;` – Nikaas Jun 09 '21 at 09:09
  • @Nikaas thanks, that’s what I was thinking too but I paused it during play and it was looking perfectly fine. I’ll try it again in the morning and maybe I’ll see something different, because it was working sometimes. – Immortal Hamster Jun 09 '21 at 09:11
  • seems that your `BulletShootPoint` rotation is not correctly aligned with the rotation of this `GunBasic` object itself ... seems to shift with the direction you look instead of where the camera looks – derHugo Jun 09 '21 at 10:10
  • @derHugo I thought that it was a rotation problem too, but now I don't think it is. I made a cube as a child of the gameobject that I shoot the raycast from, and the entire time it didn't move. The impact effect was still appearing in the wrong place about half the time, but the gameobject didn't move at all. stranger yet, when I shot at the ground three times it went back to normal. I don't know why. https://youtu.be/JxC4wq9nVRw here is the recording of it happening, it also gives a much more clear display of the problem. – Immortal Hamster Jun 09 '21 at 18:34

1 Answers1

0

It was a bug in the xr interaction toolkit. It was fixed by an update. Sorry I didn’t try this sooner, thanks for all the help.