Skip to main content
Make title specific
Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Replace Input.GetKey(KeyCode.Mouse0) with New Input System Player Attcking

added 98 characters in body
Source Link

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));

  }
}

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. 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));

  }
}

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));

  }
}
Source Link

New Input System Player Attcking

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. 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));

  }
}