0
\$\begingroup\$

I was implementing the new Input system for my game, when I saw that I still had a script in my player that wouldnt allow me to start the scene, because the script still operated with the old input manager. Its probably just because I literally ask him to do so, in line 24, I think. I was still wondering if someone could tell me, how to actually use the new input system for this script and how I would have to change the script for it. And also btw I actually want to use it with an xbox one controller, if that would change anthing. Heres the script:

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

public class PlayerAttack : MonoBehaviour{

private float timeBtwAttack;
public float startTimeBtwAttack;
PlayerControls controls;
public Transform attackPos;
public LayerMask whatIsEnemies;
//public Animator camAnim;
//public Animator playerAnim;
public float attackRangeX;
public float attackRangeY;
public int damage;

void Update(){
    
    if(timeBtwAttack <= 0){

        if(Input.GetKey(KeyCode.Mouse0)){
            //camAnim.SetTrigger("shake");
            //playerAnim.SetTrigger("attack");
            Collider2D[] enemiesToDamage = Physics2D.OverlapBoxAll(attackPos.position, new       Vector2(attackRangeX, attackRangeY), 0, whatIsEnemies);
            for (int i = 0; i < enemiesToDamage.Length; i++) {
                enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
            }
            timeBtwAttack = startTimeBtwAttack;
        }
    
} else  {          
    timeBtwAttack -= Time.deltaTime;   
 }

}void OnDrawGizmosSelected(){
        
        Gizmos.color = Color.black;
        Gizmos.DrawWireCube(attackPos.position, new Vector3(attackRangeX, attackRangeY, 1));

  }
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

If you want a quick&dirty drop-in replacement:

if( Mouse.current.leftButton.wasPressedThisFrame ) {

However, that's not how the new input system is supposed to be used. The old input system was based on the philosophy that you poll the state of the input devices during Update. The new input system is based on events. You don't ask the system what's the input state; the system tells you that an input event occurred by calling a callback method. But doing it like that requires that you completely rethink the way you handle input in your game.

What you usually have in a project using the new input system is:

  1. An Input Actions asset which allows you to define semantic input actions (like "shoot") and then assign multiple controls to them (like left mouse button, Gamepad button X, keyboard keys Space and x and pressing the touchscreen).
  2. A bunch of handler-methods on various scripts which are supposed to be called when one of those semantic input actions happens
  3. A PlayerInput component which links semantic actions to the handler-methods.

But explaining the whole thing from start to finish would be yet another Unity new input system tutorial. Others have already created enough of those which are easy to find with your preferred search engine. Or you could read the manual which also offers a pretty good introduction into how to use the system efficiently.

\$\endgroup\$
4
  • \$\begingroup\$ I actually already made a input action asset, but didnt know how to combine it with this sort of code, thanks for the help, you still really helped me with it! \$\endgroup\$
    – Hammynator
    Commented Jan 9, 2023 at 17:25
  • \$\begingroup\$ I tried using your code, but with a xbox controller and I cant seem to get it to work, I just get a error message saying gamepad does not exist in the current context. Here is the line of code, I havent changed anything else: if(gamepad.current.A.wasPressedThisFrame){ \$\endgroup\$
    – Hammynator
    Commented Jan 9, 2023 at 18:04
  • \$\begingroup\$ Did you declare a variable called "gamepad"? The engine does not declare it for you. You probably want to declare an InputAction variable representing the "Attack" action, use it to fetch the corresponding action from your Input Actions asset, and then link your gameplay to its performed event. Or create a ButtonControl and check wasPressedThisFrame. The documentation Philipp linked explains these approaches in more detail. \$\endgroup\$
    – DMGregory
    Commented Jan 9, 2023 at 19:08
  • \$\begingroup\$ @Hammynator Try Gamepad.current.aButton.wasPressedThisFrame. Or if you want to use the new way, add a PlayerInput component and use it to link the Attack action to a method. It's easier when you actually read the documentation. \$\endgroup\$
    – Philipp
    Commented Jan 9, 2023 at 23:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .