Skip to main content
13 of 15
edited tags; edited title

Changing GameObject Position with the new InputSystem Unity 2D

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.