Problem with If Statements When Comparing GO Positions

Hi guys, I’m working on a 2D rpg, and I’m trying to write a script to be held by an NPC that’ll trigger a directional animation based on where the player (dialogueTarget) is on interaction. It works perfectly along the X axis, but if you interact with the NPC from above or below, it will just play an X axis animation (left or right, depending on which dialogueTarget is closest to.)
I wrote the script from scratch, so it’s entirely possible I went about this all wrong.
I have a feeling there might be something wrong with how the If statements are structured in LookInDirection();
Could you guys take a look and let me know what you think?

private Animator anim;
     private DialogueManager dMan;
     private float difX;
     private float difY;
     public Transform dialogueTarget;
 
    Start();
    {
            anim = GetComponent<Animator>();
            dMan = FindObjectOfType<DialogueManager>();
 
            difX = transform.position.x - dialogueTarget.position.x;
            difY = transform.position.y - dialogueTarget.position.y;
    }
 
    Update();
    {
         if(dMan.dialogueActive)
               {
                  LookInDirection();
               }
    }
 
    LookInDirection();
    {
        if(Mathf.Abs(difX) < Mathf.Abs(difY))
                {
                    if(transform.position.y < dialogueTarget.position.y)
                    {
                        anim.SetFloat("x", 0);
                        anim.SetFloat("y", 1);
                    }
                    else if(transform.position.y > dialogueTarget.position.y)
                    {
                        anim.SetFloat("x", 0);
                        anim.SetFloat("y", -1);
                    }
                }
            if (Mathf.Abs(difX) > Mathf.Abs(difY))
                {
                    if(transform.position.x < dialogueTarget.position.x)
                    {
                        anim.SetFloat("x", 1);
                        anim.SetFloat("y", 0);
                    }
                    else if(transform.position.x > dialogueTarget.position.x)
                    {
                        anim.SetFloat("x", -1);
                        anim.SetFloat("y", 0);
                    }
                }
    }

I see two major places the problem could be:

  1. where you pointed at, the if statement block. But looking at that, it seems reasonable. at first glance.

  2. something in the wiring of your animator.

To isolate between these two major places, put four debug statements in the four possible ways the nested if statements can evaluate: UP, DOWN, LEFT, RIGHT

Then you can tell if the code is working and the animator is failing, or vice-versa.

1 Like

I actually just figured it out a few minutes ago! I only defined difX and difY in Start(), so it was never taking the Abs of their current positions, just their starting positions.

I just moved difX = and difY = to follow my if(dMan.dialogueActive) check in Update.

1 Like