Home>

The method of specifying the range seems to be different and it does not behave as expected
Currently, when I click twice on the mouse layer, I made it to detect two specific objects and make one attack the other object, but it seems that the range specification method is wrong. It will behave as if it attacks even though it is out of range.

The distance to the enemy is measured with the player as the main axis.
What I'm using is float attackRadius float shotRadius This is the size of a circle that the player detects the opponent.
On the other hand, detectionX detectionY specifies the range of the Physics2D.OverlapBoxAll square.
The distance calculation detects whether it is within a circle, and the OverlapBoxAll detects whether there is a specific person in the collider.
Also, with OverlapBoxAll, the collider detects if there is an enemy selected by instance ID.

If the object in the collider is detected by OverlapBoxAll in the for statement, the attack starts, and if it is not turned to the last, it is regarded as out of the attack range and it does not attack.

problem
I can't say anything because I don't know if the range itself is correct now, but it will detect if there is an enemy on a straight line from the player's point of view. Other than that, it is not detected (diagonal). The x in the image is detected ☆ is the player. The small circle is attack Radiuse and the large circle is Shot Radius.

code
    void ChangeTarget ()
    {
        if (setPlayerUnit&&setEnemyUnit)
        {
            // Get player unit components
            CharactersController charaCon = playerUnit.GetComponent<CharactersController>();
            // Determine if it is greater than the attack range and below the shooting range
            Debug.Log (Vector3.Distance (enemyUnit.transform.position, charaCon.transform.position) + "Attack Range =" + charaCon.attackRadius + "Shooting Range =" + charaCon.shotRadius);
            if (Vector3.Distance (enemyUnit.transform.position, charaCon.transform.position)>charaCon.attackRadius)
            {
                // Collider position
                Vector2 pos = new Vector2 (charaCon.transform.position.x, charaCon.transform.position.y);
                // Get the collider in the character's box judgment Is it better to judge with a circle?
                Collider2D [] hitTargets = Physics2D.OverlapBoxAll (pos, new Vector2 (charaCon.detectionX, charaCon.detectionY), enemyUnitLayer);
                for (int i = 0;i<hitTargets.Length;i ++)
                {
                    Debug.Log (hitTargets [i] .name);
                    if (enemyUnit.GetInstanceID () == hitTargets [i] .gameObject.GetInstanceID ())
                    {
                        // Insert enemy units if conditions are met
                        charaCon.target = enemyUnit;
                        // Return all to the initial values ​​Let me select againplayerUnit = null;
                        enemyUnit = null;
                        setPlayerUnit = false;
                        setEnemyUnit = false;
                        Debug.Log ("Changed player target");
                        break;
                    }
                    else if (i == hitTargets.Length --1)
                    {
                        Debug.Log ("i");
                        if (enemyUnit.GetInstanceID () == hitTargets [i] .gameObject.GetInstanceID ())
                        {
                            // Insert enemy units if conditions are met
                            charaCon.target = enemyUnit;
                            // Return all to the initial values ​​Let me select again
                            playerUnit = null;
                            enemyUnit = null;
                            setPlayerUnit = false;
                            setEnemyUnit = false;
                            Debug.Log ("Changed player target");
                            break;
                        }
                        else else
                        {
                            // Return all to the initial values ​​Let me select again
                            playerUnit = null;
                            enemyUnit = null;
                            setPlayerUnit = false;
                            setEnemyUnit = false;
                            Debug.Log ("Enemy units are out of range");
                            break;
                        }
                    }}
            }
            else else
            {
                if (Vector3.Distance (enemyUnit.transform.position, charaCon.transform.position)<= charaCon.attackRadius)
                {
                    Debug.Log ("within attack range");
                    // Return all to the initial values ​​Let me select again
                    playerUnit = null;
                    enemyUnit = null;
                    setPlayerUnit = false;
                    setEnemyUnit = false;
                }
                else if (Vector3.Distance (enemyUnit.transform.position, charaCon.transform.position)>= charaCon.shotRadius)
                {
                    Debug.Log ("Out of range");
                    // Return all to the initial values ​​Let me select again
                    playerUnit = null;
                    enemyUnit = null;
                    setPlayerUnit = false;
                    setEnemyUnit = false;
                }
            }
        }
    }

Thank you.

Also, I am trying to make the range visible,

code
Gizmos.DrawWireCube (transform.position, new Vector2 (detectionX, detectionY));


Is this right?

  • Answer # 1

    Collider2D [] hitTargets = Physics2D.OverlapBoxAll (pos, new Vector2 (charaCon.detectionX, charaCon.detectionY), enemyUnitLayer);

Related articles