Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Help with Input System

Discussion in 'Input System' started by AfterCell2, May 14, 2024.

  1. AfterCell2

    AfterCell2

    Joined:
    May 10, 2024
    Posts:
    8
    Okay so iv got my code it works even though its a mess it lets me move and the games done but when i build its not letting me move and iv searched up and it says i need to use the input system but im struggling to just change over my code to the input system and have no clue on how to do it.

    if someone can tell me how that would be great, thanks.

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.  
    3. using System.Buffers.Text;
    4. using Unity.VisualScripting.Antlr3.Runtime.Tree;
    5. using Unity.VisualScripting;
    6. using UnityEditor.Experimental.GraphView;
    7. using UnityEditor.PackageManager;
    8. using UnityEditor.VersionControl;
    9. using UnityEngine;
    10. using UnityEngine.Events;
    11. using UnityEngine.UI;
    12. using static UnityEngine.UIElements.UxmlAttributeDescription;
    13. using UnityEngine.InputSystem;
    14.  
    15. public class CharacterController2D : MonoBehaviour
    16. {
    17.     [SerializeField] private float m_JumpForce = 400f;                          // Amount of force added when the player jumps.
    18.     [Range(0, 1)][SerializeField] private float m_CrouchSpeed = .36f;           // Amount of maxSpeed applied to crouching movement. 1 = 100%
    19.     [Range(0, .3f)][SerializeField] private float m_MovementSmoothing = .05f;   // How much to smooth out the movement
    20.     [SerializeField] private bool m_AirControl = false;                         // Whether or not a player can steer while jumping;
    21.     [SerializeField] private LayerMask m_WhatIsGround;                          // A mask determining what is ground to the character
    22.     [SerializeField] private Transform m_GroundCheck;                           // A position marking where to check if the player is grounded.
    23.     [SerializeField] private Transform m_CeilingCheck;                          // A position marking where to check for ceilings
    24.     [SerializeField] private Collider2D m_CrouchDisableCollider;                // A collider that will be disabled when crouching
    25.  
    26.     const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
    27.     private bool m_Grounded;            // Whether or not the player is grounded.
    28.     const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
    29.     private Rigidbody2D m_Rigidbody2D;
    30.     private bool m_FacingRight = true;  // For determining which way the player is currently facing.
    31.     private Vector3 m_Velocity = Vector3.zero;
    32.     public CharacterController2D controller;
    33.     Vector2 inputMove = Vector2.zero;
    34.     Vector2 inputJump = Vector2.zero;
    35.     Vector2 inputTalk = Vector2.zero;
    36.     Vector2 inputAttack = Vector2.zero;
    37.  
    38.     [Header("Events")]
    39.     [Space]
    40.  
    41.     public UnityEvent OnLandEvent;
    42.  
    43.     [System.Serializable]
    44.     public class BoolEvent : UnityEvent<bool> { }
    45.  
    46.     public BoolEvent OnCrouchEvent;
    47.     private bool m_wasCrouching = false;
    48.  
    49.     private void Awake()
    50.     {
    51.         m_Rigidbody2D = GetComponent<Rigidbody2D>();
    52.  
    53.         if (OnLandEvent == null)
    54.             OnLandEvent = new UnityEvent();
    55.  
    56.         if (OnCrouchEvent == null)
    57.             OnCrouchEvent = new BoolEvent();
    58.     }
    59.  
    60.     private void FixedUpdate()
    61.     {
    62.         bool wasGrounded = m_Grounded;
    63.         m_Grounded = false;
    64.  
    65.         // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    66.         // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    67.         Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    68.         for (int i = 0; i < colliders.Length; i++)
    69.         {
    70.             if (colliders[i].gameObject != gameObject)
    71.             {
    72.                 m_Grounded = true;
    73.                 if (!wasGrounded)
    74.                     OnLandEvent.Invoke();
    75.             }
    76.         }
    77.  
    78.         // Move Our Character
    79.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    80.         jump = false;
    81.     }
    82.  
    83.  
    84.     public void Move(float move, bool crouch, bool jump)
    85.     {
    86.         // If crouching, check to see if the character can stand up
    87.         if (crouch)
    88.         {
    89.             // If the character has a ceiling preventing them from standing up, keep them crouching
    90.             if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    91.             {
    92.                 crouch = true;
    93.             }
    94.         }
    95.  
    96.         //only control the player if grounded or airControl is turned on
    97.         if (m_Grounded || m_AirControl)
    98.         {
    99.  
    100.             // If crouching
    101.             if (crouch)
    102.             {
    103.                 if (!m_wasCrouching)
    104.                 {
    105.                     m_wasCrouching = true;
    106.                     OnCrouchEvent.Invoke(true);
    107.                 }
    108.  
    109.                 // Reduce the speed by the crouchSpeed multiplier
    110.                 move *= m_CrouchSpeed;
    111.  
    112.                 // Disable one of the colliders when crouching
    113.                 if (m_CrouchDisableCollider != null)
    114.                     m_CrouchDisableCollider.enabled = false;
    115.             }
    116.             else
    117.             {
    118.                 // Enable the collider when not crouching
    119.                 if (m_CrouchDisableCollider != null)
    120.                     m_CrouchDisableCollider.enabled = true;
    121.  
    122.                 if (m_wasCrouching)
    123.                 {
    124.                     m_wasCrouching = false;
    125.                     OnCrouchEvent.Invoke(false);
    126.                 }
    127.             }
    128.  
    129.             // Move the character by finding the target velocity
    130.             Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
    131.             // And then smoothing it out and applying it to the character
    132.             m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
    133.  
    134.             // If the input is moving the player right and the player is facing left...
    135.             if (move > 0 && !m_FacingRight)
    136.             {
    137.                 // ... flip the player.
    138.                 Flip();
    139.             }
    140.             // Otherwise if the input is moving the player left and the player is facing right...
    141.             else if (move < 0 && m_FacingRight)
    142.             {
    143.                 // ... flip the player.
    144.                 Flip();
    145.             }
    146.         }
    147.         // If the player should jump...
    148.         if (m_Grounded && jump)
    149.         {
    150.             // Add a vertical force to the player.
    151.             m_Grounded = false;
    152.             m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    153.         }
    154.     }
    155.  
    156.  
    157.     private void Flip()
    158.     {
    159.         // Switch the way the player is labelled as facing.
    160.         m_FacingRight = !m_FacingRight;
    161.  
    162.         // Multiply the player's x local scale by -1.
    163.         Vector3 theScale = transform.localScale;
    164.         theScale.x *= -1;
    165.         transform.localScale = theScale;
    166.     }
    167.  
    168.     public Animator animator;
    169.  
    170.     public GameOver GameOverScreen;
    171.  
    172.  
    173.     public float runSpeed = 40f;
    174.  
    175.     float horizontalMove = 0f;
    176.     bool jump = false;
    177.     bool crouch = false;
    178.  
    179.     void Update()
    180.     {
    181.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    182.  
    183.         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    184.  
    185.         if (Input.GetButtonDown("Jump"))
    186.         {
    187.             jump = true;
    188.             animator.SetBool("IsJumping", true);
    189.         }
    190.  
    191.         if (Input.GetButtonDown("Crouch"))
    192.         {
    193.             crouch = true;
    194.         }
    195.         else if (Input.GetButtonUp("Crouch"))
    196.         {
    197.             crouch = false;
    198.         }
    199.     }
    200.  
    201.     public void OnLanding()
    202.     {
    203.         animator.SetBool("IsJumping", false);
    204.     }
    205.  
    206.     public void OnMove(InputAction.CallbackContext value)
    207.     {
    208.         inputMove = value.ReadValue<Vector2>();
    209.     }
    210.  
    211.     public void OnJump(InputAction.CallbackContext value)
    212.     {
    213.         inputJump = value.ReadValue<Vector2>();
    214.     }
    215.  
    216.     public void OnAttack(InputAction.CallbackContext value)
    217.     {
    218.         inputAttack = value.ReadValue<Vector2>();
    219.     }
    220.     public void OnTalk(InputAction.CallbackContext value)
    221.     {
    222.         inputTalk = value.ReadValue<Vector2>();
    223.     }
    224. }
    225. #endif
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,413
    Switching input systems is a LOT more than just code.

    Start from any one of the ten billion Youtube tutorials for the new input system.

    It will be extremely detail-oriented with lots of non-code steps that must be completed precisely, so work carefully.

    I suggest this guy's simple one-step-at-a-time approach:

    Imphenzia: How Did I Learn To Make Games: