0
\$\begingroup\$

Can anyone tell me what is wrong with me code? If you can see, whenever the player is in range of the raycast, the enemy turns red. But for some reason, even when the player has meet the requirement to make the enemy turn red, it turns back to white. This is only visible when you move it manually, in normal speed it looks as if it is flashing.

I believe a reason for this is happening is that, one of the raycast doesn't meet the requirement anymore. Probably something related to that. I don't know exactly.

enter image description here

This picture shows that even when the player is within the requirement of of making the enemy red. It doesn't. Why?

using UnityEngine;
using System.Collections;

/* TL = Top Left
 * TR = Top Right
 * BL = Bottom Left
 * BR = Bottom Right
 */

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 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 ();
        Player_Transform_Points_Detection ();



    }

    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() {

        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 = true;

                }

            }
            else {
                alerted = false;
            }


        }


    }

    public void detectionAlert() {
        if (alerted == true) {
            gameObject.renderer.material.color = Color.red;     
        }
        else {
            gameObject.renderer.material.color = Color.white;
        }
    }

    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;

    }



}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Remove the

        else {
            alerted = false;
        }

And move the alerted = false; to BEFORE the for-loop:

public void enemyTransform_TL_TR() {
    alerted = false;
    for (int i = 0; i < 4; i++) {
       ...

You're clearing the boolean value on the last iteration, making the previous 3 checks useless.

\$\endgroup\$
3
  • \$\begingroup\$ Im just wondering, would there be another solution to solve this ? \$\endgroup\$
    – JekasG
    Commented Jan 10, 2015 at 1:28
  • \$\begingroup\$ Add a break; right after your alerted = true; but you should still initialize alerted = false; before the for-loop. Setting it on every loop's else is a waste of CPU time. \$\endgroup\$ Commented Jan 10, 2015 at 15:59
  • \$\begingroup\$ Is that a recommended way of doing it ? Sorry, but i have a request, is it possible for you to try and answer my other question ? I am really stuck on that. Please. Here is the link. gamedev.stackexchange.com/questions/91921/… \$\endgroup\$
    – JekasG
    Commented Jan 11, 2015 at 6:20

You must log in to answer this question.

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