Search Unity

Field of view 2d side scroller visual cone raycasting problem.

Discussion in '2D' started by DoctorBronze, Oct 19, 2020.

  1. DoctorBronze

    DoctorBronze

    Joined:
    Oct 13, 2020
    Posts:
    13
    I'm currently trying to create a field of view effect which will show where the enemies are able to detect the player, i've built a special code based mesh object which stores calculates and generates meshes and the enemy object is a parent object of the said mesh object, on the enemy object i have a script which is supposed to calculate all the points which interact with objects and then the mesh object takes the said points from the script on the enemy object and inserts them in the location where they're supposeed to be in the vertices array. However when i run the game it always places the said points in the exact same spot which isn't even the spot where the raycasting hits the colliders but rather a spot which is in the middle of nowhere and as far as i can tell doesn't seem to hit anything whatsoever.

    Code (CSharp):
    1. using Platformer.Mechanics;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class RaycastingCalCulator : MonoBehaviour
    7. {
    8.     private AnimationController controller;
    9.     private float DetectionRange;
    10.     private int raycount;
    11.     public Vector3[] RayCastingArray;
    12.     public Vector3 EmptyVector;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         controller = GetComponent<AnimationController>();
    17.         DetectionRange = GetComponent<EnemyController>().DetectionRange;
    18.         raycount = GetComponent<EnemyController>().raycount;
    19.         RayCastingArray = new Vector3[raycount + 2];
    20.         EmptyVector = RayCastingArray[0];
    21.     }
    22.  
    23.     public static Vector3 GetVectorFromAngle(float angle)
    24.     {
    25.         float anglerad = angle * (Mathf.PI / 180f);
    26.         return new Vector3(Mathf.Cos(anglerad), Mathf.Sin(anglerad));
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         const float INITIAL_ANGLE = Mathf.PI / 8;
    33.         float angle = INITIAL_ANGLE;
    34.         int vertexIndex = 1;
    35.  
    36.         for (int i = 0; i <= raycount; i++)
    37.         {
    38.             Vector3 origin = transform.position;
    39.             int direction = 0;
    40.             if (controller.move.x < 0) { direction = -1; } else if (controller.move.x != 0) { direction = 1; }
    41.             // possible bug with enemies which stand still.
    42.             float vertex_x = origin.x + DetectionRange * direction;
    43.             float vertex_b = Mathf.Tan(angle) * (-transform.position.x) + transform.position.y;
    44.             float vertex_y = Mathf.Tan(angle) * vertex_x + vertex_b;
    45.             int IgnoredLayer = 1 << 2;
    46.             Vector3 vertex = new Vector3(vertex_x, vertex_y);
    47.             float distance = Vector3.Distance(vertex, origin);
    48.             RaycastHit2D raycastHit2D = Physics2D.Raycast(origin, GetVectorFromAngle(angle), distance, IgnoredLayer);
    49.             RayCastingArray[vertexIndex] = EmptyVector;
    50.  
    51.             if (raycastHit2D.collider != null)
    52.             {
    53.                 Vector3 rvertex = raycastHit2D.point;
    54.                 RayCastingArray[vertexIndex] = rvertex;
    55.                 Debug.Log(rvertex);
    56.                 Debug.Log(origin);
    57.                 Debug.DrawRay(origin, rvertex);
    58.                 Debug.Log(vertex_x);
    59.                 Debug.Log(vertex_y);
    60.             }
    61.  
    62.  
    63.             vertexIndex++;
    64.             angle -= INITIAL_ANGLE * 2 / raycount;
    65.         }
    66.     }
    67. }
    68.