5
\$\begingroup\$

Im working on a script which kinda works like an Enemy AI. Its kinda like a detection system with requirements that are required to be met.

This is a recent question i asked using my code Raycast Flashing problem

So this is the how it works:

enter image description here

using UnityEngine;
using System.Collections;

public class Script_v2 : MonoBehaviour {

    // Player Properties
    private GameObject player;
    public Vector3 playerSize;
    private Vector3 playerTransform;
    public Vector3 playerTransformTL;
    public Vector3 playerTransformTR;
    public Vector3 playerTransformBL;
    public Vector3 playerTransformBR;

    private Vector3 newPlayerTransformTL;
    private Vector3 newPlayerTransformTR;

    private Vector3[] playerRaycastPoints;


    // Enemy Properties
    private Vector3 enemyTransformTL;
    private Vector3 enemyTransformTR;
    private Vector3 enemyTransformBL;
    private Vector3 enemyTransformBR;

    public float distance;
    public Vector3 enemySize;

    // Detection Alerts
    public bool outOfVision;
    public bool alerted;
    public bool alertedLock;
    public bool dead;

    Ray ray;
    RaycastHit hit;

    // Use this for initialization
    void Start () {
        playerRaycastPoints = new Vector3[4];

        distance = 3f;
        player = GameObject.FindGameObjectWithTag ("Player");


    }

    // Update is called once per frame
    void Update () {

        enemyTransformTL = new Vector3 (transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z);
        enemyTransformTR = new Vector3 (transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z);


        enemyTransform_TL_TR ();
        detectionAlert ();
        Reference_Player_Transform_Points ();

        Debug.Log (alerted + " " + alertedLock);

    }

    void OnDrawGizmos() {
        Gizmos.color = Color.blue;
        Gizmos.DrawWireSphere (new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
        Gizmos.DrawWireSphere (new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
    }

    public void enemyTransform_TL_TR() {

        alerted = true;
        alertedLock = false;
        for (int i = 0; i < 4; i++) {

            double enemyAngleTL = Mathf.Atan2(playerRaycastPoints[i].y - ( transform.position.y + 0.5f ),
                                              playerRaycastPoints[i].x - ( transform.position.x - 0.5f )) * 180f / 3.14159265f;
            //Debug.Log (enemyAngleTL);
            double enemyAngleTR = Mathf.Atan2 (playerRaycastPoints[i].y - (transform.position.y + 0.5f),
                                               playerRaycastPoints[i].x - (transform.position.x + 0.5f)) * 180f / 3.14159265f;
            
            Vector3 directionTL = (playerRaycastPoints[i] - enemyTransformTL).normalized;
            Ray rayTL = new Ray(enemyTransformTL, directionTL);
            RaycastHit hitTL;
            Vector3 directionTR = (playerRaycastPoints[i] - enemyTransformTR).normalized;
            Ray rayTR = new Ray (enemyTransformTR, directionTR);
            RaycastHit hitTR;


            //Debug.DrawRay (rayTR.origin, rayTR.direction * distance, Color.yellow);

            if(Physics.Raycast (rayTL, out hitTL, distance)) {
                if((enemyAngleTL > 90 && enemyAngleTL < 180)) {
                    Debug.DrawRay (rayTL.origin, rayTL.direction * distance, Color.yellow);
                    alerted = false;



                }
                else {

                    alertedLock = true;
                }

            }
        }
        //alertedLock = true;
    }

    public void detectionAlert() {
        if (alerted == false && alertedLock == false) {
            gameObject.renderer.material.color = Color.green;
        }
        if (alerted == false && alertedLock == true) {
            gameObject.renderer.material.color = Color.magenta;     
        }
        if(alerted == true && alertedLock == false) {
            gameObject.renderer.material.color = Color.blue;
        }
        if(alerted == true && alertedLock == true) {
            gameObject.renderer.material.color = Color.red;
        }
        else {
            //gameObject.renderer.material.color = Color.red;
        }
    }

    private void Reference_Player_Transform_Points() {

        playerSize = player.transform.localScale;

        playerTransformTL = new Vector3(player.transform.position.x - (playerSize.x / 2),
                                        player.transform.position.y + playerSize.y  / 2,
                                        player.transform.position.z);
        playerTransformTR = new Vector3(player.transform.position.x + (playerSize.x / 2),
                                        player.transform.position.y + playerSize.y  / 2,
                                        player.transform.position.z);
        playerTransformBL = new Vector3(player.transform.position.x - (playerSize.x / 2),
                                        player.transform.position.y - playerSize.y  / 2,
                                        player.transform.position.z);
        playerTransformBR = new Vector3(player.transform.position.x + (playerSize.x / 2),
                                        player.transform.position.y - playerSize.y  / 2,
                                        player.transform.position.z);

        playerRaycastPoints [0] = playerTransformTL;
        playerRaycastPoints [1] = playerTransformTR;
        playerRaycastPoints [2] = playerTransformBL;
        playerRaycastPoints [3] = playerTransformBR;

        /*
            Debug.Log (playerTransformTL);
            Debug.Log (playerTransformTR);
            Debug.Log (playerTransformBL);
            Debug.Log (playerTransformBR);
        */
    }
}

The problem with this code is that. If you look in the diagram. alertLock should be false when its outside the greenZone.

But for some reason it doesnt. When the player is fully inside the greenZone, the enemy turns green. I dont know why.

\$\endgroup\$
3
  • \$\begingroup\$ The documentation for Physics.Raycast says: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour. \$\endgroup\$ Commented Dec 28, 2014 at 16:01
  • \$\begingroup\$ The sphere is only for better understanding visually. \$\endgroup\$
    – JekasG
    Commented Dec 28, 2014 at 16:03
  • \$\begingroup\$ Might I ask what your inspector variables are? Such as playerSize, distance, enemySize? Also, could I see a screen cap of your Physics Settings? Finally, are you raycasting against colliders or trigger colliders? \$\endgroup\$ Commented Aug 28, 2015 at 20:00

1 Answer 1

0
\$\begingroup\$

I think you have a typo in detectionAlert()...

(alerted == false && alerted == false) should be (alerted == false && alertedLock == false)

\$\endgroup\$
1
  • \$\begingroup\$ I fixed this typo and it still doesnt solve the problem \$\endgroup\$
    – JekasG
    Commented Jan 4, 2015 at 14:33

Not the answer you're looking for? Browse other questions tagged .