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
Related articles
- c # - i added collider with particles to the object (prefab?) of the game (cities: skylines) using unity, but it is not reflecte
- [c #] in unity, i want to repeatedly move the cube object to the z axis, but a variable conversion error occurs
- c # - i want to display the information of the object at the mouse position in another place in unity
- in ruby, i want to make a hash using the range object element as a key
- c # - i want to rotate the gyro sensor of the smartphone and the object of the smartphone on the scene in sync with unity
- c # - is the corner of the scaffolding object a 3d model jumped in unity? if you touch, you will fly to an abnormal height
- [unity, c #] how to specify the top object
- c # - object duplication script in unity 2d
- c # - i want to send an object from view to controller
- c # - [unity] i want to store the transformpositiony of the dynamically generated object in a list or array and get the maximum
- c # - pun2 suddenly destroy object
- c # - i want to reflect object data in view
- c # - i want to make the current angle of the object in unity the angle of the entered value (in world coordinates)
- google api - how to get range object when changing multiple cells by gas event trigger "when changing"
- c # - i want to move an object to the center of the screen in unityvr vr space
- c # - i don't understand object orientation
- c # - ray's hitpoint with a specific object
- python - regarding the detection range in object detection (yolov3)
- c # - about setting clone object variables
- c # - player production in unity (shooting game)
- c # - i want to display high score and score in unity
- c # - [unity] format exception: input string was not in a correct format error
- c # - i want sloths to move with the ferris wheel gondola (i want to add inertia)
- c # - moving the unity scene doesn't switch as intended
- c # - unity ugui i want to call the process when an overlapping object is clicked
- c # - please tell me how to use the additional components of unity's volunteer production
- c # - i want to display at the previous scroll position when reopening in the scroll view of unity
- c # - unity2d 2d action suddenly i can't move horizontally
- c # - i want to slowly rotate the model to a fixed angle when the flag is set, and slowly return it to the original angle when t
Collider2D [] hitTargets = Physics2D.OverlapBoxAll (pos, new Vector2 (charaCon.detectionX, charaCon.detectionY), enemyUnitLayer);