1

Okay, so I believe this concept should be simple. However, the answer is eluding me. Editor background. The "other" object and the gameObject with this code both have sphere colliders which are as triggers, this gameObject has a CharacterController attached for movement, but neither have RigidBodies. My goal is for this (the Player object, moving around automatically PacMan style) to kill things it runs in to, but only if it is facing them, otherwise, it should take damage. My issue is figuring out how to tell if it was facing the object. To complete the picture, moveDirection can be one of the following (Vector3.Up, .Down, .Left or .Right) as the Player and NPC's move along the X and Y axes only (Orthographic Camera with 3D game objects)

~ OLD METHOD

    void OnTriggerEnter (Collider other)
    {

        if (other.gameObject.tag == "Human") {
            RaycastHit hit;
            if (Physics.Raycast (transform.position, moveDirection, out hit)) {
                StartCoroutine (eatWithBlood (1, other.gameObject.GetComponent<NPCController> ()));
            } else {
                TakeDamage (other.gameObject.GetComponent<NPCController> ().attack);
            }

        } 
    }

I've also tried the following code, which works a little better. The math doesn't work quite right, so even if the angles are between 45 and -45, the player still gets "stunned" and takes damage. The new method is preconfigured to expand should I need the other variables for more accurate calculations. the faceDirection variables are up=0, right=1, down=2, left=3 which also correspond to their Vector3.up, left, down and right cohorts.

~ NEW METHOD

void OnTriggerEnter (Collider other)
    {

        if (other.gameObject.tag == "Human") {
            float angle = Vector3.Angle (transform.forward, other.gameObject.transform.position);
            Debug.Log ("Angle: " + angle + "; my forward: " + faceDirection + "; other forward: " + other.gameObject.GetComponent<NPCController> ().faceDirection + " ... " + Time.time);
            if (IAteHim (faceDirection, other.gameObject.GetComponent<NPCController> ().faceDirection, angle)) {
                other.gameObject.GetComponent<NPCController> ().EatMe ();
                StartCoroutine (eatWithBlood (1, other.gameObject.GetComponent<NPCController> ()));
            } else {
                GameObject poof = PoofPooler.poof.GetPooledObject ();
                if (poof != null) {
                    poof.transform.position = Vector3.Lerp (transform.position, other.gameObject.transform.position, 0.5f);
                    poof.GetComponent<PoofControl> ().reactivated = true;
                    poof.SetActive (true);

                }
                other.gameObject.GetComponent<NPCController> ().MakeInvincible ();
                TakeDamage (other.gameObject.GetComponent<NPCController> ().attack, 2.0f);
            }


        }
    }

    bool IAteHim (int myDirection, int otherDirection, float angle)
    {
        if (angle > -45 && angle < 45) { // I'm facing toward yummies
            return true;
        } else {
            return false;
        }

    }
Hawk_Pilot
  • 135
  • 3
  • 8

1 Answers1

0

change

float angle = Vector3.Angle (transform.forward, other.gameObject.transform.position);

to

Vector3 direction = other.gameObject.transform.position - transform.position;
float angle = Vector3.Angle (transform.forward, direction);
Jinjinov
  • 2,554
  • 4
  • 26
  • 45