Cast Rays to check collisions in Front of a GameObject?

Hi! I have a Rail Shooter (House of the Dead-Like), and i Have an issue.
As I have multiple enemies that aproach the static player using NavMesh and NavMeshAgent, I need to make them stop if they have another enemy in front of them, as they will constantly push eachother to get through.
I tried several things as BoxCast, ShpereCast, CheckShpere… but they dont work as I detroy the enemies when they get shot, so any OnTriggerExit wont work.
My final idea is to cast several Rays from the enemy to his front to see if any other enemy is there, and tell him to stop moving until there’s none. So I must cast Rays in different ANGLES at the same time.
My problem is: how do i change the Ray’s angle?
I tried:

        RaycastHit hit1;
        var direction1 = transform.forward;
        Physics.Raycast(transform.position, direction1, out hit1, 5);

        RaycastHit hit2;
        var direction2 = transform.forward*Quaternion.AngleAxis(30,transform.forward);
        Physics.Raycast(transform.position, direction2, out hit1, 5);

But is says i cant MULTIPLY Vector3 * Quaternion.

Any help or ideas would be appreciated.

Thank you!

Lisandro, from Argentina.

var direction2 = transform.forward*Quaternion.AngleAxis(30,transform.up);

Rotate forward around up axis by 30 degrees.
(Rotate forward vector around itself yields no difference, output still is forward)

Oh yes right, it should be transform.up.
Nevertheless Visual Studio Says I cant multiply Vector3 *(per) Quaternion.:frowning:

It’s the other way around, sorry.
Quaternion * Vector3

Right! I will try this and let the Fourm know! Be right back!

Thank you Thermal!

Edit: It works but it detects collision all the time, wich stops all my enemies… thats wierd because i only cast 7 rays in these angles: 0, 10, 20, 30, -10, -20 and -30.

Also when I draw the line it draws a line in the bottom of the object (the ground)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayCreator : MonoBehaviour {

    Animator animatorEnemy;
    EnemyMovement enemy;
    LayerMask layermaskEnemy = ~10;
    Vector3 rayPos;

    void Start ()
    {
        animatorEnemy = GetComponent<Animator>();
        enemy = GetComponent<EnemyMovement>();
        rayPos = new Vector3(transform.position.x+0.35f, transform.position.y + 1, transform.position.z);

    }
   
    void Update ()
    {       
        RaycastHit hit1;
        var direction1 = transform.forward;
      //Physics.Raycast(transform.position, direction1, out hit1, 3,layermaskEnemy);
        RaycastHit hit2;
        var direction2 = Quaternion.AngleAxis(10, transform.up)*transform.forward;
       //Physics.Raycast(transform.position, direction2, out hit2, 3, layermaskEnemy);
        RaycastHit hit3;
        var direction3 = Quaternion.AngleAxis(20, transform.up) * transform.forward;
       //Physics.Raycast(transform.position, direction3, out hit3, 3, layermaskEnemy);
        RaycastHit hit4;
        var direction4 = Quaternion.AngleAxis(30, transform.up) * transform.forward;
       //Physics.Raycast(transform.position, direction4, out hit4, 3, layermaskEnemy);
        RaycastHit hit5;
        var direction5 = Quaternion.AngleAxis(-10, transform.up) * transform.forward;
       //Physics.Raycast(transform.position, direction5, out hit5, 3, layermaskEnemy);
        RaycastHit hit6;
        var direction6 = Quaternion.AngleAxis(-20, transform.up) * transform.forward;
        //Physics.Raycast(transform.position, direction6, out hit6, 3, layermaskEnemy);
        RaycastHit hit7;
        var direction7 = Quaternion.AngleAxis(-30, transform.up) * transform.forward;
        //Physics.Raycast(transform.position, direction7, out hit7, 3, layermaskEnemy);
       
        if (Physics.Raycast(rayPos, direction1, out hit1, 4, layermaskEnemy) || Physics.Raycast(rayPos, direction2, out hit2, 4, layermaskEnemy) ||
           Physics.Raycast(rayPos, direction3, out hit3, 4, layermaskEnemy) || Physics.Raycast(rayPos, direction4, out hit4, 4, layermaskEnemy) ||
           Physics.Raycast(rayPos, direction5, out hit5, 4, layermaskEnemy) || Physics.Raycast(rayPos, direction6, out hit6, 4, layermaskEnemy) ||
           Physics.Raycast(rayPos, direction7, out hit7, 4, layermaskEnemy))
        {
            Debug.DrawRay(transform.position, transform.forward, Color.green);
            Debug.Log("CHOQUE CONTRA ENEMIGO");
            animatorEnemy.SetFloat("Speed", 0);
            enemy.isStopped = true;
            return;
        }
        else
        {
            Debug.DrawRay(transform.position, transform.forward, Color.green);
            animatorEnemy.SetFloat("Speed", 0.1f);
            enemy.isStopped = false;
        }
    }
}

You should really replace all that duplication with a loop.
It’s hard to tell exactly what happens from the code. I see you create a rayPos at start, but I can’t see you update it.
Your debug rays also don’t start at rayPos but the raycasts do.
Make sure the rays don’t start in the ground or hit their own enemy.

I think this is the problem, the ray touches de enemy where its origin is, so the enemy stops when it shouldn’t.
I added the rayPos line in the Update so it changes every frame but this still happens

The weird thing is that in rayPos i wrote that it should put the ray’s origin in transfrom.position.x+0.35f and it does not. I even tried changing that number to bigger ones like transfrom.position.x+10 but the ray keeps originating at the same spot… help!!