Advertisement
Dieton

SimpleNetPlayer

Jun 1st, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | Gaming | 0 0
  1. using Mirror;
  2. using UnityEngine;
  3. using Cinemachine;
  4. using UnityEngine.InputSystem;
  5.  
  6. public class SimpleNetPlayer : NetworkBehaviour
  7. {
  8.     //-- player movement --//
  9.     public float playerMoveSpeed = 2.0f;
  10.     Vector2 PlayerMovementInputVector;
  11.     float moveX;
  12.     float moveZ;
  13.  
  14.     //-- Input Variables --//
  15.     private PlayerInput playerInput;
  16.     private MyInputActions myInputActions;
  17.  
  18.     // -- Animation --//
  19.     public Animator animator;
  20.  
  21.  
  22.     //==== On Start Local Player ====//
  23.     public override void OnStartLocalPlayer()
  24.     {
  25.         //setup Player input
  26.         PlayerInputSetup();
  27.  
  28.         // Find player camera and tell it to look at this player
  29.         GameObject.Find("PlayerFollowCamera").GetComponent<CinemachineVirtualCamera>().Follow = this.gameObject.transform.GetChild(0).transform;
  30.         Camera.main.transform.SetParent(transform);
  31.         Camera.main.transform.localPosition = new Vector3(0, 0, 0);
  32.     }
  33.  
  34.     //==== Input Sytem ====//
  35.     public void PlayerInputSetup()
  36.     {
  37.         playerInput = GetComponent<PlayerInput>();
  38.         myInputActions = new MyInputActions();
  39.         myInputActions.Player.Enable();
  40.     }
  41.  
  42.     //==== Update ====//
  43.     //[Client]
  44.     private void Update()
  45.     {
  46.         if (!isLocalPlayer)
  47.         {
  48.             return;
  49.         }
  50.  
  51.         PlayerMove();
  52.     }
  53.  
  54.     //==== Player Move ====//
  55.     public void PlayerMove()
  56.     {
  57.         PlayerMovementInputVector = myInputActions.Player.Move.ReadValue<Vector2>();
  58.         moveX = PlayerMovementInputVector.x * Time.deltaTime * 110.0f;// _input.move.x
  59.         moveZ = PlayerMovementInputVector.y * Time.deltaTime * playerMoveSpeed;//_input.move.y
  60.         transform.Translate(0, 0, moveZ);
  61.         transform.Rotate(0, moveX, 0);
  62.  
  63.         if (isLocalPlayer)
  64.         {
  65.             animator.SetFloat("Speed", PlayerMovementInputVector.y);
  66.         }
  67.     }
  68. }
  69.  
Tags: Unity mirror
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement