Search Unity

Third Party Mirror possible incompatibility with New Input System?

Discussion in 'Multiplayer' started by DylanPotts, Apr 14, 2022.

  1. DylanPotts

    DylanPotts

    Joined:
    May 13, 2021
    Posts:
    3
    I'm getting this error in Unity after pressing the relative "eastButton":

    Error: MissingReferenceException while executing 'canceled' callbacks of 'Gameplay/Block[/DualShock4GamepadHID/buttonEast,/Mouse/rightButton]' UnityEngine.InputSystem.InputActionAsset:OnDestroy () (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Actions/InputActionAsset.cs:794)

    I'm attempting to set up 'block' functionality within my game which simply locks the player's movement then disables my "canBeHit" bool which will then put the player in a state where they cannot be hit, however upon interacting with the eastButton all player movement is completely locked preventing you from moving, however rotation is still possible. Another part I'd like to note is that I'm working with the Mirror package so I can network my game, unsure if this could also be causing compatibility problems. Would really appreciate if someone could point out where I've gone wrong, I'd be very grateful.

    PlayerMovementTest.cs

    Code (CSharp):
    1.     using System.Collections.Generic;
    2.     using UnityEngine;
    3.     using UnityEngine.InputSystem;
    4.     using Mirror;
    5.    
    6.     public class PlayerMovementTest : NetworkBehaviour
    7.     {
    8.    
    9.         [Header ("Default")]
    10.         Rigidbody rb;
    11.         PlayerInput playerInput;
    12.         PlayerControls playerInputActions;
    13.         public bool isStunned;
    14.         public float stunLockTimer;
    15.         public float stunLockTimerEnd;
    16.         public Animator animations;
    17.         public ParticleSystem stunParticle;
    18.    
    19.         [Header("Movement")]
    20.         public float speed;
    21.         public float baseSpeed;
    22.    
    23.         [Header ("Dodge")]
    24.         public float dodgeMultiplier;
    25.         public float dodgeTimer;
    26.         public float dodgeTimerMax;
    27.         public int dodgeCheck;
    28.         public float howLongDodgeLast;
    29.         public bool isDodging;
    30.         public GameObject enemyPlayer;
    31.         public GameObject dodgeParticle;
    32.    
    33.         [Header("Attack")]
    34.         public int attackReset;
    35.         public float attackTimer;
    36.         public float attackTimerResetMax;
    37.         public float attackPlayingTimerEnd;
    38.         public float lungeAttackLungeMultiplier;
    39.         public bool isAttacking;
    40.         public GameObject attackParticle;
    41.         public GameObject attackHitboxObject;
    42.         public GameObject lungeAttackHitboxObject;
    43.    
    44.    
    45.         [Header("Block")]
    46.         public bool canBeHit;
    47.    
    48.         private void Awake()
    49.         {
    50.    
    51.             playerInput = GetComponent<PlayerInput>();
    52.             rb = GetComponent<Rigidbody>();
    53.             playerInputActions = new PlayerControls();
    54.    
    55.             // freezes rotation
    56.             rb.freezeRotation = true;
    57.             // enables the input controller
    58.             playerInputActions.Gameplay.Enable();
    59.          
    60.             playerInputActions.Gameplay.Attack.performed += Attack;
    61.             playerInputActions.Gameplay.Attack.canceled += Attack;
    62.             playerInputActions.Gameplay.Dodge.performed += Dodge;
    63.             playerInputActions.Gameplay.Block.canceled += Block;
    64.             playerInputActions.Gameplay.Block.performed += Block;
    65.        
    66.    
    67.             isStunned = false;
    68.         }
    69.    
    70.         public void Update()
    71.         {
    72.    
    73.             if (!isLocalPlayer)
    74.                 return;
    75.    
    76.             // dodge timers and reset
    77.             if (dodgeCheck != 1)
    78.                 dodgeTimer += Time.deltaTime;
    79.    
    80.             if (dodgeTimer > dodgeTimerMax)
    81.             {
    82.                 dodgeCheck += 1;
    83.                 dodgeTimer = 0;
    84.             }
    85.    
    86.             // turns on the slide particle when sliding
    87.             if (isDodging == true)
    88.                 dodgeParticle.SetActive(enabled);
    89.             else
    90.                 dodgeParticle.SetActive(false);
    91.    
    92.             // sets how long the EFFECT of the dash lasts for
    93.             // not how long the player will move but how long
    94.             // they will be able to collide with another player
    95.             // and stun them
    96.             if (dodgeTimer > howLongDodgeLast)
    97.                 isDodging = false;
    98.    
    99.             // attack timers and reset
    100.             if (attackReset != 1)
    101.             {
    102.                 attackTimer += Time.deltaTime;
    103.             }
    104.    
    105.             // after a certian amount of time the player attack goes away
    106.             if (attackTimer > attackPlayingTimerEnd)
    107.             {
    108.                 //attackParticle.SetActive(false);
    109.                 attackHitboxObject.SetActive(false);
    110.                 lungeAttackHitboxObject.SetActive(false);
    111.    
    112.             }
    113.    
    114.             if (attackTimer > attackTimerResetMax)
    115.             {
    116.                 attackReset += 1;
    117.                 attackTimer = 0;
    118.             }
    119.    
    120.             // makes it so attacks dont go into the minuses
    121.             if (attackReset < 0)
    122.                 attackReset = 0;
    123.    
    124.             // when player gets stunned it starts a timer where they cannot do any moves
    125.             if (isStunned == true)
    126.             {
    127.                 stunLockTimer += Time.deltaTime;
    128.    
    129.                 if (stunLockTimer > stunLockTimerEnd)
    130.                 {
    131.                     isStunned = false;
    132.                     stunLockTimer = 0f;
    133.                     speed = baseSpeed;
    134.                 }
    135.             }
    136.    
    137.             // chanegs between animations depending on how fast the player is moving
    138.             if (rb.velocity.magnitude > 4f)
    139.                 animations.SetFloat("Walk", rb.velocity.magnitude);
    140.           if (rb.velocity.magnitude < 4f)
    141.                 animations.SetFloat("Idle", rb.velocity.magnitude);
    142.          
    143.    
    144.                 // if player is stunned then play stun particle
    145.                 if (isStunned == true)
    146.                 stunParticle.Play();
    147.             else
    148.                 stunParticle.Stop();
    149.    
    150.    
    151.            //edited out the print as it got distracting
    152.            // print(rb.velocity.magnitude);
    153.         }
    154.    
    155.         // for dodge stun
    156.         public void OnCollisionEnter(Collision col)
    157.          {
    158.             // if you hit a player
    159.            if( col.gameObject.tag == "Player")
    160.            {
    161.                 // and in dodge, number picked just as an idea of how long the dash could last
    162.                 if (dodgeTimer > .1f && dodgeTimer < 1f)
    163.                 {
    164.                     // sets the player hit as a gameobject that cabn be affected in script
    165.                     enemyPlayer = col.gameObject;
    166.                     // decreases speed (or in this case increases as i cannot test 2 players
    167.                     enemyPlayer.GetComponent<PlayerMovementTest>().speed = 0;
    168.                     enemyPlayer.GetComponent<PlayerMovementTest>().isStunned = true;
    169.                 }
    170.            }
    171.         }
    172.    
    173.         public void FixedUpdate()
    174.         {
    175.             // calls movement function
    176.             Movement();
    177.         }
    178.    
    179.         public void Attack(InputAction.CallbackContext context)
    180.         {
    181.      
    182.             if (attackReset == 1 && isStunned == false)
    183.             {
    184.                 isAttacking = true;
    185.                 attackHitboxObject.SetActive(enabled);
    186.                 attackReset -= 1;
    187.                 animations.SetTrigger("Attack");
    188.                 attackParticle.SetActive(enabled);
    189.             }
    190.    
    191.             //if (attackReset == 1 && isStunned == false)
    192.    
    193.             /*
    194.             if (context.performed)
    195.                 {
    196.                     print("attack start");
    197.                 }
    198.                 if (context.canceled)
    199.                 {
    200.                 // for luneg attack
    201.                     rb.AddForce(transform.forward * lungeAttackLungeMultiplier, ForceMode.Impulse);
    202.                 isAttacking = true;
    203.                 lungeAttackHitboxObject.SetActive(enabled);
    204.                 attackReset -= 1;
    205.                // animations.SetTrigger("Attack");
    206.                 //   attackParticle.SetActive(enabled);
    207.                     print("attack end");
    208.                 }*/
    209.         }
    210.    
    211.         public void Dodge(InputAction.CallbackContext context)
    212.         {
    213.             // checks if the player has a dodge
    214.             if (dodgeCheck == 1 && isStunned == false)
    215.             {
    216.                 // used for lockign slide in place and particle effect
    217.              isDodging = true;
    218.              // adds a force to the player, spped can be adjusted with dodgeMultiplier
    219.              rb.AddForce(transform.forward * dodgeMultiplier, ForceMode.Impulse);
    220.              // removes dodge
    221.              dodgeCheck -= 1;
    222.              animations.SetTrigger("Slide");
    223.             }
    224.         }
    225.    
    226.         public void Block(InputAction.CallbackContext context)
    227.         {
    228.            if (isStunned == false)
    229.            {     // when you hold the button, you lose your speed
    230.             if (context.performed)
    231.             {
    232.                 canBeHit = false;
    233.                 speed = 3;
    234.                 animations.SetBool("Block", true);
    235.             }
    236.        
    237.             // when you release it your speed goes back to normal
    238.             if (context.canceled)
    239.             {
    240.                 canBeHit = true;
    241.                 speed = 15;
    242.                 animations.SetBool("Block", false);
    243.                 }
    244.             }
    245.         }
    246.    
    247.         public void Movement()
    248.         {
    249.             // used for locking slide in place, no rotation
    250.            if (isDodging == false)
    251.            {
    252.                 //player movement, can only use vector2 for controller so we use a vector3
    253.                 // but store the x and z in a vector 2
    254.                 Vector2 inputVector = playerInputActions.Gameplay.Walk.ReadValue<Vector2>();
    255.                 Vector3 tempVec = new Vector3(inputVector.x, 0, inputVector.y);
    256.            
    257.                     // adds force to the vector, do this seperately so we can use
    258.                     //the variable for the player rotation
    259.                     rb.AddForce(tempVec * speed, ForceMode.Force);
    260.              
    261.                 if (tempVec != Vector3.zero)
    262.                 {
    263.                 // finds the direction the player is moving
    264.                 Quaternion targetRotation = Quaternion.LookRotation(tempVec);
    265.                 // rotates players towards the way they are facing
    266.                 targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 * Time.fixedDeltaTime);
    267.    
    268.                 rb.MoveRotation(targetRotation);
    269.                 }
    270.             }
    271.        
    272.         }
    273.     }
    274.  
    If any additional information is needed to solve my problem I'm more than happy to submit any necessary info.
     
  2. Ashfid

    Ashfid

    Joined:
    Jun 8, 2019
    Posts:
    20
    It’s not input system issue. Try unsubscribing your events in OnDisable or OnDestroy (Gameplay.Dodge.canceled -= Block), you should always unsubscribe your events.