Search Unity

Discussion Help, Input System Rebind Dosen't WORK

Discussion in 'Input System' started by felix16072010, Jul 12, 2023.

  1. felix16072010

    felix16072010

    Joined:
    Oct 18, 2022
    Posts:
    1
    Hi, I recently followed some tutorials on how to use the new Input System package's Sample Rebind. I tried to put it to work but it doesn't work. after a while I downloaded a project already done with the same Rebind System with the only difference of the Controller. I wonder if there is some problem in my Code. Thanks to anyone who will help me.

    The Code of The Player controller:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Drawing;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class FPS : MonoBehaviour
    8. {
    9.     public float speed = 5.0f;
    10.     public float normalspeed = 5.0f;
    11.     public float runSpeed = 10.0f; // Velocità di corsa
    12.     private float jumpHeight = 2.0f;
    13.     public float normaljumpHeight = 2.0f;
    14.     public float gravity = -9.81f;
    15.     public Transform groundCheck;
    16.     public float groundDistance = 0.4f;
    17.     public LayerMask groundMask;
    18.     public Transform cameraTransform;
    19.     public int Health = 100;
    20.     public float ShurikenTime = 5f;
    21.     public bool CanThrow = true;
    22.  
    23.     [Range(10.0f, 50.0f)]
    24.     public float mouseSensitivity = 20.0f;
    25.  
    26.     [Range(10, 100)]
    27.     public int FOV = 70;
    28.  
    29.     public GameObject bullet;
    30.     public Transform Spawner;
    31.     public float crouchHeight = 1.0f; // Nuova altezza del personaggio quando è accovacciato
    32.     public float crouchSmoothTime = 0.1f; // Tempo di transizione per l'animazione di accovacciamento
    33.  
    34.     private CharacterController controller;
    35.     private Vector3 velocity;
    36.     private bool isGrounded;
    37.     private bool isCrouching;
    38.     private bool isRunning; // Variabile per tenere traccia dello stato di corsa del personaggio
    39.     private float xRotation = 0.0f;
    40.     private float originalHeight;
    41.     private float crouchVelocity; // Velocità di transizione per l'animazione di accovacciamento
    42.     Animator anim;
    43.     private float randomFloat;
    44.     public PlayerInput playerControls;
    45.  
    46.     //inizialize
    47.     private InputAction move;
    48.     private InputAction fire;
    49.     private InputAction look;
    50.     private InputAction jump;
    51.     private InputAction sprint;
    52.     private InputAction fire1;
    53.  
    54.     void Start()
    55.     {
    56.         controller = GetComponent<CharacterController>();
    57.         Cursor.lockState = CursorLockMode.Locked;
    58.         originalHeight = controller.height;
    59.         anim = GetComponent<Animator>();
    60.         speed = normalspeed;
    61.         jumpHeight = normaljumpHeight;
    62.     }
    63.  
    64.     private void Awake()
    65.     {
    66.         playerControls = new PlayerInput();
    67.     }
    68.  
    69.     private void OnEnable()
    70.     {
    71.         move = playerControls.Player.Move;
    72.         move.Enable();
    73.  
    74.         look = playerControls.Player.Look;
    75.         look.Enable();
    76.  
    77.         fire = playerControls.Player.Fire;
    78.         fire.Enable();
    79.         fire.performed += Fire;
    80.  
    81.         fire1 = playerControls.Player.Fire1;
    82.         fire1.Enable();
    83.         fire1.performed += Fire1;
    84.  
    85.         jump = playerControls.Player.Jump;
    86.         jump.Enable();
    87.         jump.performed+= Jump;
    88.  
    89.         sprint = playerControls.Player.Sprint;
    90.         sprint.Enable();
    91.         sprint.performed += Sprint;
    92.         sprint.canceled += RelaseSprint;
    93.     }
    94.  
    95.     //private void OnDisable()
    96.     //{
    97.     //    move.Disable();
    98.     //    look.Disable();
    99.     //    fire.Disable();
    100.     //    fire1.Disable();
    101.     //    jump.Disable();
    102.     //    sprint.Disable();
    103.     //}
    104.  
    105.     void Update()
    106.     {
    107.         GameObject AttackState = GameObject.Find("Attack(Clone)");
    108.  
    109.         // Controlla se l'oggetto esiste
    110.         if (AttackState != null)
    111.         {
    112.             // Fai qualcosa con l'oggetto
    113.             Health -= 10;
    114.             Debug.Log("-10");
    115.             Destroy(AttackState);
    116.         }
    117.  
    118.         if (Health <= 0)
    119.         {
    120.             SceneManager.LoadScene(0);
    121.         }
    122.  
    123.         randomFloat = Random.Range(0, 2);
    124.         // Verifica se il personaggio è a contatto con il suolo
    125.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    126.         cameraTransform.GetComponent<Camera>().fieldOfView = FOV;
    127.  
    128.         // Movimento orizzontale
    129.         //float x = Input.GetAxis("Horizontal");
    130.         //float z = Input.GetAxis("Vertical");
    131.  
    132.         // Verifica se il personaggio sta correndo
    133.         //if (Input.GetButton("Sprint"))
    134.         //{
    135.         //    isRunning = true;
    136.         //    anim.SetFloat("RunSpeed", 2);
    137.         //}
    138.         //else
    139.         //{
    140.         //    isRunning = false;
    141.         //    anim.SetFloat("RunSpeed", 1);
    142.         //}
    143.  
    144.  
    145.         // Calcola la nuova altezza del personaggio in base allo stato di accovacciamento
    146.         float targetHeight = isCrouching ? crouchHeight : originalHeight;
    147.         // Esegue una transizione fluida verso la nuova altezza del personaggio
    148.         controller.height = Mathf.SmoothDamp(controller.height, targetHeight, ref crouchVelocity, crouchSmoothTime);
    149.  
    150.         //if (Input.GetButtonDown("Jump") && isGrounded)
    151.         //{
    152.         //    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    153.         //    anim.SetTrigger("Jump");
    154.         //    anim.SetBool("Waking", true);
    155.         //}
    156.  
    157.         //if (Input.GetButtonDown("Fire1") && !anim.GetCurrentAnimatorStateInfo(0).IsName("Punch"))
    158.         //{
    159.         //    Debug.Log(randomFloat);
    160.         //    anim.SetFloat("CPunch", randomFloat);
    161.         //    anim.SetTrigger("Punch");
    162.         //     Abilita la collisione di tutti gli oggetti con il tag "MyTag"
    163.  
    164.         //}
    165.  
    166.         //if (Input.GetButtonDown("Fire2") && !anim.GetCurrentAnimatorStateInfo(0).IsName("Throw"))
    167.         //{
    168.         //    anim.SetTrigger("Throw");
    169.         //    StartCoroutine(Shuriken());
    170.  
    171.         //}
    172.  
    173.  
    174.         // Movimento del personaggio
    175.         Vector3 moveDir = move.ReadValue<Vector2>().x * transform.right + move.ReadValue<Vector2>().y * transform.forward;
    176.  
    177.  
    178.         controller.Move(moveDir * (isRunning ? runSpeed : speed) * Time.deltaTime);
    179.         velocity.y += gravity * Time.deltaTime;
    180.         controller.Move(velocity * Time.deltaTime);
    181.  
    182.         // Movimento della telecamera
    183.         float mouseX = look.ReadValue<Vector2>().x * mouseSensitivity * Time.deltaTime;
    184.         float mouseY = look.ReadValue<Vector2>().y * mouseSensitivity * Time.deltaTime;
    185.  
    186.         xRotation -= mouseY;
    187.         xRotation = Mathf.Clamp(xRotation, -90, 90);
    188.  
    189.         cameraTransform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    190.         transform.Rotate(Vector3.up * mouseX);
    191.  
    192.         // Verifica se il personaggio si sta muovendo
    193.         if (moveDir.magnitude > 0)
    194.         {
    195.             // Esegui le azioni desiderate quando il personaggio si sta muovendo
    196.             anim.SetBool("Waking", true); // Imposta il parametro "isMoving" sull'animator a true
    197.         }
    198.         else
    199.         {
    200.             // Esegui le azioni desiderate quando il personaggio non si sta muovendo
    201.             anim.SetBool("Waking", false); // Imposta il parametro "isMoving" sull'animator a false
    202.         }
    203.     }
    204.  
    205.  
    206.     private void Fire(InputAction.CallbackContext contex)
    207.     {
    208.         if(!anim.GetCurrentAnimatorStateInfo(0).IsName("Punch"))
    209.         {
    210.             Debug.Log(randomFloat);
    211.             anim.SetFloat("CPunch", randomFloat);
    212.             anim.SetTrigger("Punch");
    213.         }
    214.  
    215.     }
    216.  
    217.     private void Fire1(InputAction.CallbackContext contex)
    218.     {
    219.         if(!anim.GetCurrentAnimatorStateInfo(0).IsName("Throw") && CanThrow)
    220.         {
    221.             CanThrow= false;
    222.             anim.SetTrigger("Throw");
    223.             StartCoroutine(Shuriken());
    224.         }
    225.     }
    226.  
    227.     private void Jump(InputAction.CallbackContext contex)
    228.     {
    229.         if (isGrounded)
    230.         {
    231.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    232.             anim.SetTrigger("Jump");
    233.             anim.SetBool("Waking", true);
    234.         }
    235.     }
    236.  
    237.     private void Sprint(InputAction.CallbackContext contex)
    238.     {
    239.         isRunning = true;
    240.         anim.SetFloat("RunSpeed", 2);
    241.     }
    242.  
    243.     private void RelaseSprint(InputAction.CallbackContext contex)
    244.     {
    245.         isRunning = false;
    246.         anim.SetFloat("RunSpeed", 1);
    247.     }
    248.  
    249.     IEnumerator ShurikenCooldown()
    250.     {
    251.  
    252.         yield return new WaitForSeconds(ShurikenTime);
    253.         CanThrow = true;
    254.     }
    255.  
    256.     IEnumerator Shuriken()
    257.     {
    258.         yield return new WaitForSeconds(0.56f);
    259.         GameObject bulletInstance = Instantiate(bullet, Spawner.position, Spawner.rotation);
    260.         System.Array.ForEach(bulletInstance.GetComponentsInChildren<MeshCollider>(), x => x.enabled = false);
    261.         System.Array.ForEach(bulletInstance.GetComponentsInChildren<MeshCollider>(), x => x.enabled = true);
    262.         bulletInstance.GetComponent<Rigidbody>().AddForce(transform.forward * 20, ForceMode.Impulse);
    263.         StartCoroutine(ShurikenCooldown());
    264.         yield return new WaitForSeconds(2f);
    265.         Destroy(bulletInstance);
    266.     }
    267. }