Simple raycast not working...

Hello. I tried fixing the code and looking through everything and I still have the same problem as I did last time.

RaycastHit hit;
           
            if(Physics.Raycast(coneOfVision.transform.position, enemy.transform.position, out hit, sightDistance))
            {
                Debug.Log ("Raycast Shot");
                Debug.DrawLine (coneOfVision.transform.position, enemy.transform.position, Color.green, sightDistance);
                if(hit.collider.gameObject.tag == "TeamTwo" )
                {
                    Debug.Log("Raycasy hit");
                    pointValue += 1f;
                }
            }

The drawline appears to be hitting the TeamTwo gameObject but Raycast hit never shows up except on 2 very rare occasions and I could not figure out why.

The other object is tagged as TeamTwo.

I have been able to get other gameObjects such as AI soldiers to register the hit, but not this one which is a tank.

the sightDistance variable is not an issue because I have also tried MathF.Infinity

is there any other collider being hit by the ray? you could try using RaycastAll instead and see if that yields more consistent results.

Vector3 norm = (enemy.transform.position - coneOfVision.transform.position).normalized;
Raycast[] hits = Physics.RaycastAll(coneOfVision.transform.position, norm, sightDistance);
if(hits.Length > 0)
{
    foreach(RaycastHit hit in hits)
    {
        if(hit.collider.gameObject.tag == "TeamTwo")
        {
            Debug.Log("Raycast hit");
            pointValue += 1f;
            break;
        }
    }
}

Its the weirdest thing, I used a direction normalized like your norm vector3 in the raycast, (enemy.transform.position - coneOfVision.transform.position) and if I start the tank facing the TeamTwo object, it will register that it is indeed hitting the correct object, then after awhile it will start hitting the object behind it…

messing around with the colliders size and soldiers size has some results but they are inconsistent.

I have a StanceChecker() function on a script on my soldier that is called every update function. I am wondering if that has anything to do with it.

here is the relevant code

private CapsuleCollider collider;

void Start ()
    {
        collider = GetComponent<CapsuleCollider>();
    }

void Update ()
    {
        StanceChecker();
    }

void StanceChecker()
    {
      
        //Check and set crouch and prone animator bools
        if(crouch == true)
        {
            anim.SetBool ("Crouch", true);
            collider.center = new Vector3(0,0.6f,0);
            collider.radius = 0.3f;
            collider.height = 1.3f;
        }
        if(crouch != true)
        {
            anim.SetBool ("Crouch", false);
            collider.center = new Vector3(0,0.9f,0);
            collider.radius = 0.2f;
            collider.height = 1.7f;
      
        }
}

I am wondering if this is relevant because I get a yellow message about that script saying:
‘SoldierAnimator.collider’ hides inherited member ‘UnityEngine.Component.collider’. Use the new keyword if hiding was intended.

and I couldn’t find any information about what that means… although the raycast issue is not a problem with soldiers targeting other soldiers, it is only a problem with this tank I am trying to build… and I even tried using the same code.

I did in fact fix the yellow collider error by changing the name of the variable to col… it didn’t fix the raycast issue.

I unchecked the IsTrigger on the TeamTwo soldier and now it only works if the soldier is standing… it doesn’t work when the soldier is crouching though…

Ok I got it, since I resized the soldier I had to re-edit the scripts that move the “AimHere” gameobject so the tank aims lower and into the collider.

Is it normal for a raycast to only recognize the triggers for a few seconds, is it usually more accurate to not use trigger colliders?