I am unsure I perceive your query 100%, however for the goal of attacking left, you possibly can merely deal with the Participant
as an entire and flip it horizontally. In 2D video games we regularly use scale(-1,1) to do that:
public Rework participant;
...
personal void OnMove(InputValue worth)
{
moveInputValue = worth.Get<Vector2>();
participant.localScale = new Vector2((moveInputValue.x > 0?1:-1),1);
}
edit:
After some restricted observations, I lastly perceive what you need. Now let’s make a listing of what has been achieved and what want to be achieved.
Achieved:
- Participant shifting.
- Injury Dealing.
- Attacking settle down.
To be achieved:
- The assault path is decided by the motion path(360°). This consists of the place of the assault level and the rotation of the form used for detection.
To attain the player-centered rotation of the assault level, the worth that the developer must set is now not a hard and fast level(attackPos
), however a hard and fast distance(attackRange
).
// public Rework attackPos;
public float attackRange; // substitute attackPos with attackRange
The calculation of the precise place follows the components:
attackPos = playerPos + normalized(attackDirection)*attackRange
normalized
means preserve the path of the vector and alter its size to 1.
vector*
quantity means preserve the path of the vector and multiply its size.
normalized(attackDirection)*attackRange
means a vector with a size of attackRange within the attackDirection, it is a relative worth, so we have to add the place vector of the participant itself.
So now we will do that:
var attackPos = (Vector2)remodel.place + attackDir*attackRange; //Get the world coordinates of attackPos
var enemiesToDamage = Physics2D.OverlapBoxAll(
attackPos,
new Vector2(attackRangeX, attackRangeY),
0,
whatIsEnemies
);
*You should use the built-in member variable remodel
to get the remodel of the gameobject the place the present script is positioned.
How can we get attackDir
? It primarily is determined by the path of motion, so simply use the normalized worth of moveInputValue
. However when the motion stops, the worth of moveInputValue
is Vector2.zero
, which prevents the assault from executing accurately. When the participant stops shifting, we have to get the final shifting path because the assault path. We are able to set a non-zero preliminary worth for attackDir
, and solely when moveInputValue
just isn’t 0, overwrite attackDir
with its normalized worth.
personal Vector2 moveInputValue;
personal Vector2 attackDir = Vector2.proper; // unbiased path of assault
personal void OnMove(InputValue worth)
{
moveInputValue = worth.Get<Vector2>();
if(moveInputValue != Vector2.zero){
attackDir = moveInputValue.normalized; //When the participant stops shifting, use the final motion path because the assault path
}
}
Now let’s take a look at rotation. We are able to use Vector2.Angle and Vector2.SignedAngle to get the angle between two vectors. So we will calculate the angle between a vector and a vector with an angle of 0 to get the angle worth of this vector:
var angle = Vector2.SignedAngle(Vector2.up,attackDir); //Solid Vector2 to angle[-180,180], it is utilized in OverlapBoxAll
Or you should utilize trigonometric capabilities to determine the angle of a vector:
public float Angle(Vector2 vector2)
{
return 360 - (Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg * Mathf.Signal(vector2.x));
}
Lastly, we’d like a visible debugging technique. Unity doesn’t have an api for DrawBox
with a rotation angle. You’ll be able to debug it by no matter you want, comparable to calculating the coordinates of the 4 factors of the rectangle and drawing 4 line segments, or utilizing a sprite object as a debugger.
BTW, If you calculate the speed and apply it to the inflexible physique, you do not want Time.fixedDeltaTime
, This a part of the calculation is dealt with by the physics part of the engine itself.
personal void MoveLogicMethod()
{
// Vector2 consequence = moveInputValue * pace * Time.fixedDeltaTime;
Vector2 consequence = moveInputValue.normalized * pace; //fixedDeltaTime just isn't wanted right here
rb2D.velocity = consequence;
}