using multiple raycasts to detect obstacles?

I wish to make a script where the player sprite moves around and when it touches specific objects it stops while when it touches others it moves past them. this can be done with oncollisionenter or ontriggerenter but this is a something I want to do with purely scripting and no other components other than colliders.
my current way of doing this is casting 5 rays in the direction the sprite is moving and act according to what is in front. my problem is that I cannot calculate the way to make 5 rays appear in every direction (8 directions including the diagonals because that is how my game works). this is the basic idea that I have:

    public float Speed;
    public Transform PlayerTransform;
    public Sprite PlayerCharacter;

    LayerMask lm = new LayerMask();
    RaycastHit2D[] ForwardRay = new RaycastHit2D[5];
    Vector2 dir;

    int verticalMov = 1;
    int horizontalMov = 0;

    void Update()
    {
        if (Input.GetKey(Up)) verticalMov++; // keys pressed
        if (Input.GetKey(Down)) verticalMov--;
        if (Input.GetKey(Left)) horizontalMov--;
        if (Input.GetKey(Right)) horizontalMov++;

        dir = new Vector2(horizontalMov, verticalMov); // calculate direction of movement

        ForwardRay = new RaycastHit2D[5];

        int j = 0;

        for (int i = ForwardRay.Length / -2; i < ForwardRay.Length / 2 + 0.5; i++)
        {
            ForwardRay[j] = Physics2D.Raycast(new Vector2(PlayerTransform.position.x + (PlayerCharacter.bounds.size.x / (ForwardRay.Length - 1) * i * dir.x), PlayerTransform.position.y + (PlayerCharacter.bounds.size.y / (ForwardRay.Length - 1) * i * dir.y)), dir, 0.5f, lm); // calculation that does not work

            Debug.DrawRay(new Vector2(PlayerTransform.position.x + (PlayerCharacter.bounds.size.x / (ForwardRay.Length - 1) * i), PlayerTransform.position.y + (PlayerCharacter.bounds.size.y / (ForwardRay.Length - 1) * i)), dir, Color.red, Mathf.Infinity); // debugging

            if (ForwardRay[j] && ForwardRay[j].collider)
            {
                if (ForwardRay[j].collider.gameObject.layer == 7)
                {
                    dir = Vector2.zero; // stop moving if hit an obstacle
                }
            }
            j++;
        }

        PlayerTransform.Translate(dir.normalized * Speed * Time.deltaTime); // calculate movement
    }

my problem is that I cannot find a way to calculate and cast the 5 rays in all 8 directions the player can point to. if anyone has any ideas or answers please help.

Any Vector3 can be rotated by a Quaternion.

For instance, if you have a Quaternion:

Quaternion myRotation = Quaternion.Euler( 0, 45, 0);

You can rotate any Vector3 by 45 degrees around the y:

Vector3 originalVector =  ... however you get this

Vector3 rotatedBy45Vector = myRotation * originalVector;
1 Like
        float angle=-20; 
        for (int i=0; i<5; i++)
        {
            Vector2 forwardRay=dir;
            forwardRay=Quaternion.AngleAxis(angle, Vector3.forward) * forwardRay;   // rotate forwardRay
            if (Physics2D.Raycast(playerTransform.position, forwardRay, 0.5f, lm)
            {
                dir = Vector2.zero; // stop moving if hit an obstacle
                break;  // exit this loop. no need to waste time checking the remaining directions
            }
            angle+=10; // add 10 degrees to angle
        }
        PlayerTransform.Translate(dir.normalized * Speed * Time.deltaTime);
1 Like

thank you very much!
it seems I was in a completely wrong thought process.