Search Unity

Question (mirror network)client can't move but host is normal

Discussion in 'Multiplayer' started by szszszszszsz, Mar 4, 2024.

  1. szszszszszsz

    szszszszszsz

    Joined:
    Feb 27, 2024
    Posts:
    1
    my client "isLocalPlayer" always is False
    please help me

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class playermovement : NetworkBehaviour
    7. {
    8.     [Header("Movement")]
    9.     public float moveSpeed;
    10.     public float groundDrag;
    11.     public float jumpForce;
    12.     public float jumpCooldown;
    13.     public float airMultiplier;
    14.     bool readyToJump;
    15.  
    16.     [Header("Ground Check")]
    17.     public float playerHeight;
    18.     public LayerMask whatIsGround;
    19.     bool grounded;
    20.  
    21.     [Header("Keybinds")]
    22.     public KeyCode jumpKey = KeyCode.Space;
    23.  
    24.     public Transform orientation;
    25.  
    26.     float horizontalInput;
    27.     float verticalInput;
    28.  
    29.     Vector3 moveDirection;
    30.  
    31.     Rigidbody rb;
    32.  
    33.     // Start is called before the first frame update
    34.     private void Start()
    35.     {
    36.         readyToJump = true;
    37.         rb = GetComponent<Rigidbody>();
    38.         rb.freezeRotation = true;
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     private void Update()
    43.     {
    44.      
    45.         if (isLocalPlayer)
    46.         {
    47.             // ground check
    48.             grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
    49.  
    50.             MyInput();
    51.  
    52.             // handle drag
    53.             if (grounded)
    54.                 rb.drag = groundDrag;
    55.             else
    56.                 rb.drag = 0;
    57.         }
    58.     }
    59.  
    60.     private void FixedUpdate()
    61.     {
    62.      
    63.         if (isLocalPlayer)
    64.         {
    65.            
    66.             MovePlayer();
    67.         }
    68.        
    69.     }
    70.     public override void OnStartLocalPlayer()
    71.     {
    72.         Debug.Log($"[Client] OnStartLocalPlayer called. isLocalPlayer: {isLocalPlayer}, netId: {netId}");
    73.         base.OnStartLocalPlayer();
    74.        
    75.     }
    76.  
    77.     public override void OnStartClient()
    78.     {
    79.         Debug.Log($"OnStartClient called. isLocalPlayer: {isLocalPlayer}");
    80.         base.OnStartClient();
    81.        
    82.     }
    83.  
    84.     public override void OnStartServer()
    85.     {
    86.         Debug.Log("OnStartServer called.");
    87.         base.OnStartServer();
    88.        
    89.     }
    90.  
    91.  
    92.     private void MyInput()
    93.     {
    94.         horizontalInput = Input.GetAxisRaw("Horizontal");
    95.         verticalInput = Input.GetAxisRaw("Vertical");
    96.  
    97.         // when to jump
    98.         if (Input.GetKey(jumpKey) && readyToJump && grounded)
    99.         {
    100.             readyToJump = false;
    101.  
    102.             Jump();
    103.  
    104.            
    105.             Invoke(nameof(ResetJump), jumpCooldown);
    106.         }
    107.     }
    108.  
    109.     private void MovePlayer()
    110.     {
    111.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    112.  
    113.         if (grounded)
    114.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    115.         else
    116.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    117.     }
    118.  
    119.     private void Jump()
    120.     {
    121.         // reset y velocity
    122.         rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    123.  
    124.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    125.     }
    126.  
    127.     private void ResetJump()
    128.     {
    129.         readyToJump = true;
    130.     }
    131. }
    132.  
    upload_2024-3-4_19-1-48.png upload_2024-3-4_19-2-19.png
     
  2. Borland13337

    Borland13337

    Joined:
    Mar 14, 2017
    Posts:
    2
    repeated all your actions isLocalPlayer always true
     

    Attached Files:

    • 123.JPG
      123.JPG
      File size:
      24.8 KB
      Views:
      11