Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

unity 2019 input system and pun 2

Discussion in 'Input System' started by strongbox3d, Aug 3, 2020.

  1. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    816
    Hello guys,

    I hope someone can help with this issue. I am making a multiplayer game using unity 2019.4 and pun 2(photon network). I am trying to use the new input system but when I spawn players in the network, the input controls move and rotate all the players but with the old input system everything works.

    In this script(see below) the commented lines are the lines I use while trying to use the new input system(no working) and the uncommented ones are the ones I use for old input system(working perfectly).

    Code (CSharp):
    1.  
    2. using Photon.Pun;
    3. using UnityEngine;
    4.  
    5. public class CharacterInputManager : MonoBehaviourPun
    6. {
    7.     Transform myTrans;
    8.     CharacterController controller;
    9.     Animator myAnimator;
    10.     float playerSpeed = 2.0f;
    11.     float playerRotSpeed = 90.0f;
    12.  
    13.     // FOR NEW INPUT SYSTEM ONLY
    14.  
    15.     //GamepadControls controls;//New input system only
    16.     //Vector2 moving;//New input system only
    17.     //Vector2 rotating;//New input system only
    18.  
    19.  
    20.     private void Awake()
    21.     {
    22.         myTrans = GetComponent<Transform>();
    23.         controller = GetComponent<CharacterController>();
    24.         myAnimator = GetComponent<Animator>();
    25.     }
    26.  
    27.     // FOR NEW INPUT SYSTEM ONLY
    28.  
    29.     //private void OnEnable()//New input system only
    30.     //{
    31.     //    if (photonView.IsMine)
    32.     //    {
    33.     //        controls = new GamepadControls();//New input system only
    34.     //        controls.Enable();
    35.     //        controls.Player.PlayerMovement.performed += ctx => moving = ctx.ReadValue<Vector2>();
    36.     //        controls.Player.PlayerMovement.canceled += ctx => moving = Vector2.zero;
    37.     //        controls.Player.PlayerRotation.performed += ctx => rotating = ctx.ReadValue<Vector2>();
    38.     //        controls.Player.PlayerRotation.canceled += ctx => rotating = Vector2.zero;
    39.     //    }
    40.     //}
    41.  
    42.     //private void OnDisable()//New input system only
    43.     //{
    44.     //    if (photonView.IsMine)
    45.     //    {
    46.     //        controls.Disable();
    47.     //    }
    48.     //}
    49.  
    50.     private void Update()
    51.     {
    52.         if (photonView.IsMine)
    53.         {
    54.             MovePlayer();
    55.             RotatePlayer();
    56.             Animate();
    57.         }
    58.     }
    59.  
    60.     void MovePlayer()
    61.     {
    62.         // FOR NEW INPUT SYSTEM ONLY
    63.  
    64.         //controller.Move(myTrans.forward * moving.y * Time.deltaTime * playerSpeed);//New Input system only
    65.         //controller.Move(myTrans.right * moving.x * Time.deltaTime * playerSpeed);//New Input system only
    66.  
    67.         controller.Move(myTrans.forward * Input.GetAxisRaw("LeftVertical") * Time.deltaTime * playerSpeed);
    68.         controller.Move(myTrans.right * Input.GetAxisRaw("LeftHorizontal") * Time.deltaTime * playerSpeed);
    69.     }
    70.  
    71.     void RotatePlayer()
    72.     {
    73.         // FOR NEW INPUT SYSTEM ONLY
    74.  
    75.         //float rot;
    76.         //if (moving.y < -0.1f)
    77.         //{
    78.         //    rot = -rotating.x;
    79.         //}
    80.         //else
    81.         //{
    82.         //    rot = rotating.x;
    83.         //}
    84.         //myTrans.Rotate(0.0f, rot * Time.deltaTime * playerRotSpeed, 0.0f);
    85.  
    86.         float rot;
    87.  
    88.         if (Input.GetAxisRaw("LeftVertical") < -0.1f)
    89.         {
    90.             rot = -Input.GetAxisRaw("RightHorizontal");
    91.         }
    92.         else
    93.         {
    94.             rot = Input.GetAxisRaw("RightHorizontal");
    95.         }
    96.         myTrans.Rotate(0.0f, rot * Time.deltaTime * playerRotSpeed, 0.0f);
    97.     }
    98.  
    99.     void Animate()
    100.     {
    101.         ///FOR NEW INPUT SYSTEM ONLY
    102.  
    103.         //myAnimator.SetFloat("SurfaceSpeed", moving.y);
    104.         //myAnimator.SetFloat("TurnSpeed", rotating.x);
    105.  
    106.         myAnimator.SetFloat("SurfaceSpeed", Input.GetAxisRaw("LeftVertical"));
    107.         myAnimator.SetFloat("TurnSpeed", Input.GetAxisRaw("RightHorizontal"));
    108.     }
    109. }
    110.  
    111.  
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,477
    Yeah, this has caught quite a few people unawares. The gist is that the multiplayer support that's available with PlayerInput is not currently available with the generated C# wrappers. Requires some custom scripting.

    I've posted some hopefully helpful tips here.

    ////EDIT: Adding support for control schemes and InputUser to the generated C# classes is on the list.
     
  3. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    816
    Rene-Damm,

    Thank you for the response! I still don't see what/how to change the generated script to fit the needs for a Photon Network multiplayer game in your tips. Could you be kind enough to explain in more details what to change or how to fix the problem. Photon has "photonView.IsMine" as a way to know if the player is the local player. Since my game is not a local multiplayer game but a networked one, and in my script above I only initiate the script containing the inputs if the player is local, how come my input script still controls gameobjects that don't have the input script activated on them?

    I would appreciate very much if you could explain a little more/better since I am not the brightest of people.

    Regards,
    Carlos
     
unityunity