Search Unity

Discussion Error in the AI

Discussion in 'ML-Agents' started by ahmadmuddassir635, Jan 6, 2024.

  1. ahmadmuddassir635

    ahmadmuddassir635

    Joined:
    Jul 16, 2023
    Posts:
    24
    The following is the code of my AI

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

    [ExecuteInEditMode]
    public class AiSensor : MonoBehaviour
    {
    public float distance;
    public float angle;
    public float height;
    public Color meshColor;
    public int scanFrequency;
    public LayerMask layers;
    public LayerMask occulisionLayers;
    public Gun gunScript;
    public List<GameObject> objects = new List<GameObject>();

    Collider[] colliders = new Collider[50];
    Mesh mesh;
    int count;
    float scanInterval;
    float scanTimer;
    // Start is called before the first frame update
    void Start()
    {
    scanInterval = 1 / scanFrequency;
    }

    public int Filter(GameObject[] buffer, string layerName)
    {
    int layer = LayerMask.NameToLayer(layerName);
    int count = 0;
    foreach (var obj in objects)
    {
    if(obj.layer == layer)
    {
    buffer[count++] = obj;
    }

    if(buffer.Length == count)
    {
    break;
    }
    }
    return count;
    }

    // Update is called once per frame
    void Update()
    {
    scanTimer -= Time.deltaTime;
    if(scanTimer < 0)
    {
    scanTimer += scanInterval;
    Scan();
    }
    }

    private void Scan()
    {
    count = Physics.OverlapSphereNonAlloc(transform.position, distance, colliders, layers, QueryTriggerInteraction.Collide);
    objects.Clear();
    for (int i = 0; i < count; i++)
    {
    GameObject obj = colliders.gameObject;

    if (isInSight(obj))
    {
    Debug.Log("Working");
    gunScript.CoroutinesCall();
    }
    }

    }

    public bool isInSight(GameObject obj)
    {
    Vector3 origin = transform.position;
    Vector3 dest = obj.transform.position;
    Vector3 direction = dest - origin;
    if (direction.y < 0 || direction.y > height)
    {
    return false;
    }

    direction.y = 0;
    float deltaAngle = Vector3.Angle(direction, transform.forward);
    if (deltaAngle > angle)
    {
    return false;
    }

    origin.y += height / 2;
    dest.y = origin.y;
    if (Physics.Linecast(origin, dest, occulisionLayers))
    {
    return false;
    }

    return true;
    }

    Mesh CreateWedgeMesh()
    {
    Mesh mesh2 = new Mesh();
    int segments = 10;
    int numTriangles = (segments * 4) + 2 + 2;
    int numVertices = numTriangles * 3;

    Vector3[] vertices = new Vector3[numVertices];
    int[] triangles = new int[numVertices];

    Vector3 bottomCenter = Vector3.zero;
    Vector3 bottomLeft = Quaternion.Euler(0, -angle, 0) * Vector3.forward * distance;
    Vector3 bottomRight = Quaternion.Euler(0, angle, 0) * Vector3.forward * distance;

    Vector3 topCenter = bottomCenter + Vector3.up * height;
    Vector3 topRight = bottomRight + Vector3.up * height;
    Vector3 topLeft = bottomLeft + Vector3.up * height;

    int vert = 0;

    //Left

    vertices[vert++] = bottomCenter;
    vertices[vert++] = bottomLeft;
    vertices[vert++] = topLeft;

    vertices[vert++] = topLeft;
    vertices[vert++] = topCenter;
    vertices[vert++] = bottomCenter;

    //Right
    vertices[vert++] = bottomCenter;
    vertices[vert++] = topCenter;
    vertices[vert++] = topRight;

    vertices[vert++] = topRight;
    vertices[vert++] = bottomRight;
    vertices[vert++] = bottomCenter;

    float currentAngle = -angle;
    float deltaAngle = (angle * 2) / segments;
    for (int i = 0; i < segments; ++i)
    {
    bottomLeft = Quaternion.Euler(0, currentAngle, 0) * Vector3.forward * distance;
    bottomRight = Quaternion.Euler(0, currentAngle + deltaAngle, 0) * Vector3.forward * distance;

    topRight = bottomRight + Vector3.up * height;
    topLeft = bottomLeft + Vector3.up * height;
    //Far
    vertices[vert++] = bottomLeft;
    vertices[vert++] = bottomRight;
    vertices[vert++] = topRight;

    vertices[vert++] = topRight;
    vertices[vert++] = topLeft;
    vertices[vert++] = bottomLeft;

    //Top

    vertices[vert++] = topCenter;
    vertices[vert++] = topLeft;
    vertices[vert++] = topRight;

    //Bottom
    vertices[vert++] = bottomCenter;
    vertices[vert++] = bottomRight;
    vertices[vert++] = bottomLeft;
    currentAngle += deltaAngle;
    }



    for (int i = 0; i < numVertices; ++i)
    {
    triangles = i;
    }

    mesh2.vertices = vertices;
    mesh2.triangles = triangles;
    mesh2.RecalculateNormals();
    return mesh2;
    }

    private void OnValidate()
    {
    mesh = CreateWedgeMesh();
    }

    private void OnDrawGizmos()
    {
    if (mesh)
    {
    Gizmos.color = meshColor;
    Gizmos.DrawMesh(mesh, transform.position, transform.rotation);
    }

    Gizmos.DrawWireSphere(transform.position, distance);
    for (int i = 0; i < count; ++i)
    {
    Gizmos.DrawSphere(colliders.transform.position, 0.2f);
    }

    Gizmos.color = Color.black;
    foreach (var obj in objects)
    {
    Gizmos.DrawSphere(obj.transform.position, 0.2f);
    }
    }
    }
    The error is this that when the human character is enters the circle then it is working but when the player changes into the sphere or cube or cylinder then it is not working and also the project is like this that i want to change character from one to other but when it is the human character then it is working but when it is other character then it is not working. So anyone can solve this.
     
    Last edited: Jan 6, 2024