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).
using Photon.Pun;
using UnityEngine;
public class CharacterInputManager : MonoBehaviourPun
{
Transform myTrans;
CharacterController controller;
Animator myAnimator;
float playerSpeed = 2.0f;
float playerRotSpeed = 90.0f;
// FOR NEW INPUT SYSTEM ONLY
//GamepadControls controls;//New input system only
//Vector2 moving;//New input system only
//Vector2 rotating;//New input system only
private void Awake()
{
myTrans = GetComponent<Transform>();
controller = GetComponent<CharacterController>();
myAnimator = GetComponent<Animator>();
}
// FOR NEW INPUT SYSTEM ONLY
//private void OnEnable()//New input system only
//{
// if (photonView.IsMine)
// {
// controls = new GamepadControls();//New input system only
// controls.Enable();
// controls.Player.PlayerMovement.performed += ctx => moving = ctx.ReadValue<Vector2>();
// controls.Player.PlayerMovement.canceled += ctx => moving = Vector2.zero;
// controls.Player.PlayerRotation.performed += ctx => rotating = ctx.ReadValue<Vector2>();
// controls.Player.PlayerRotation.canceled += ctx => rotating = Vector2.zero;
// }
//}
//private void OnDisable()//New input system only
//{
// if (photonView.IsMine)
// {
// controls.Disable();
// }
//}
private void Update()
{
if (photonView.IsMine)
{
MovePlayer();
RotatePlayer();
Animate();
}
}
void MovePlayer()
{
// FOR NEW INPUT SYSTEM ONLY
//controller.Move(myTrans.forward * moving.y * Time.deltaTime * playerSpeed);//New Input system only
//controller.Move(myTrans.right * moving.x * Time.deltaTime * playerSpeed);//New Input system only
controller.Move(myTrans.forward * Input.GetAxisRaw("LeftVertical") * Time.deltaTime * playerSpeed);
controller.Move(myTrans.right * Input.GetAxisRaw("LeftHorizontal") * Time.deltaTime * playerSpeed);
}
void RotatePlayer()
{
// FOR NEW INPUT SYSTEM ONLY
//float rot;
//if (moving.y < -0.1f)
//{
// rot = -rotating.x;
//}
//else
//{
// rot = rotating.x;
//}
//myTrans.Rotate(0.0f, rot * Time.deltaTime * playerRotSpeed, 0.0f);
float rot;
if (Input.GetAxisRaw("LeftVertical") < -0.1f)
{
rot = -Input.GetAxisRaw("RightHorizontal");
}
else
{
rot = Input.GetAxisRaw("RightHorizontal");
}
myTrans.Rotate(0.0f, rot * Time.deltaTime * playerRotSpeed, 0.0f);
}
void Animate()
{
///FOR NEW INPUT SYSTEM ONLY
//myAnimator.SetFloat("SurfaceSpeed", moving.y);
//myAnimator.SetFloat("TurnSpeed", rotating.x);
myAnimator.SetFloat("SurfaceSpeed", Input.GetAxisRaw("LeftVertical"));
myAnimator.SetFloat("TurnSpeed", Input.GetAxisRaw("RightHorizontal"));
}
}