Change melee range based on where player is moving.

I am making a little top-down 2D RPG right now and I’m struggling with the range of my attacks. The problem is that my range is stuck on the right side of my player. This is of course really annoying, because you can’t attack to the left.

I want to make a script that makes the range transform, based on where you are facing. I’m just not experienced enough to know how.

Maybe creating a script that changes the whole movement of the player, wouldn’t be a bad idea. It would of course be nicer to just add a few things to fix the problem, but I would also be ok with changing the overall movement as a solution, I guess.

Here is the Movement + PlayerAttack script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Move : MonoBehaviour
{   

public int damage;
private float timeBtwAttack;
public float startTimeBtwAttack;



public float attackCooldown;
float _lastAttackTime;

public Transform attackPos;
public LayerMask whatIsEnemies;
public float attackRangeX;
public float attackRangeY;
public float ValueIsGiven;


//Move Start
[SerializeField] private Rigidbody2D rb2D;
[SerializeField] private float speed;
private Vector2 moveInputValue;

private void OnMove(InputValue value)
{
    moveInputValue = value.Get<Vector2>();
    if(ValueIsGiven >= 1){
    Debug.Log(moveInputValue);
    }
}
private void MoveLogicMethod()
{
    Vector2 result = moveInputValue * speed * Time.fixedDeltaTime;
    rb2D.velocity = result;
}
    private void FixedUpdate()
    {
        MoveLogicMethod();
    }
//Move End

//Attack Start

public void OnPlayerAttacking()
{
// If attacking too close to previous attack, ignore this input.
if (Time.time - _lastAttackTime < attackCooldown) return;


var enemiesToDamage = Physics2D.OverlapBoxAll(
           attackPos.position,
           new Vector2(attackRangeX, attackRangeY),
           0,
           whatIsEnemies
);

foreach (var enemy in enemiesToDamage) {
     enemy.GetComponent<Enemy>().TakeDamage(damage);
}

_lastAttackTime = Time.time;
}
void OnDrawGizmosSelected(){
        
        Gizmos.color = Color.black;
        Gizmos.DrawWireCube(attackPos.position, new Vector3(attackRangeX, attackRangeY, 1));
}
//Attack End
}

I don’t use any rotation in my movement script.

Here is a screenshot of my scene with the range:
enter image description here

The most important thing right now is to get it to work, the visuals are second.

Also the AttackRangePos is currently defined by an empty game object named “attackPos”. (You can also see it in the screenshot I took). I think some people could get confused that the Sword is the attack position, but the Sword is only for visuals. Would still be essential for the sword to move too.

I also developed an idea, which could maybe deal with this problem:
enter image description here
The idea is basically that the attackPos gets the MoveValue, so it knows where the player is currently moving and then uses this information to move to the side of the player which is facing the direction of where the player is going. Since the attackPos is parented in the Player, the attackPos only needs to be moved by 1-2 units at max, so it shouldnt be to performance consuming, i hope.

I also want the attackPos to stay at its current position, even when you are not currently moving anymore.

What you should do is rewrite the attack collision to use the player’s transform.forward or maybe transform.up, been a while since I did top down, that way the attack will always be in front of the player no matter which way it’s facing. Or you could make another gameObject for attack range and attach it to your player in the scene hierarchy, you put it where you want your attack relative to your player and it will rotate with your player. You can use it’s transform.position for your attack range.