Search Unity

Third Party Unity3D Photon Player Look not working properly

Discussion in 'Multiplayer' started by chefCoder, May 8, 2021.

  1. chefCoder

    chefCoder

    Joined:
    May 24, 2020
    Posts:
    8
    Sorry if the title sounds weird, didn't exactly know how to word this... My problem seems to be that, my player controller got broken whilst trying to add some code to make it so everyone controls their own player, as before anybody could control your player and you could control their player so I found the code to fix that, but it caused a new problem, my player can't jump or look from left to right anymore... Any help would be greatly appreciated! (Below are some code tags)
    - Chef
    MouseLook.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class MouseLook : MonoBehaviourPunCallbacks
    7. {
    8.  
    9.      public float mouseSensitivity = 100f;
    10.  
    11.      public Transform playerBody;
    12.  
    13.      float xRotation = 0f;
    14.  
    15.      CharacterController Cc;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         Cc = GetComponent<CharacterController>();
    21.         playerBody = GetComponentInChildren<Camera>().transform;
    22.         if (photonView.IsMine)
    23.         {
    24.             GetComponentInChildren<Camera>().enabled = true;
    25.             GetComponentInChildren<AudioListener>().enabled = true;
    26.         }
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.  
    33.         if (photonView.IsMine)
    34.         {
    35.             Look();
    36.         }
    37.  
    38.         void Look()
    39.         {
    40.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    41.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    42.  
    43.         xRotation -= mouseY;
    44.         xRotation = Mathf.Clamp(xRotation, -90, 90f);
    45.  
    46.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    47.         playerBody.Rotate(Vector3.up * mouseX);
    48.         }
    49.        
    50.     }
    51.  
    52. }
    PlayerMovement.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class PlayerMovement : MonoBehaviourPunCallbacks
    7. {
    8.  
    9.     public CharacterController controller;
    10.  
    11.     public float speed = 8f;
    12.     public float gravity = -9.81f;
    13.     public float jumpHeight = 3f;
    14.  
    15.     public Transform groundCheck;
    16.     public float groundDistance = 0.4f;
    17.     public LayerMask groundMask;
    18.  
    19.     Vector3 velocity;
    20.     bool isGrounded;
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.        if (photonView.IsMine)
    26.        {
    27.            Move();
    28.        }
    29.  
    30.        void Move()
    31.        {
    32.        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    33.  
    34.        if(isGrounded && velocity.y < 0)
    35.        {
    36.            velocity.y = -2f;
    37.        }
    38.  
    39.         float x = Input.GetAxis("Horizontal");
    40.         float z = Input.GetAxis("Vertical");
    41.  
    42.         Vector3 move = transform.right * x + transform.forward * z;
    43.  
    44.         controller.Move(move * speed * Time.deltaTime);
    45.  
    46.         if(Input.GetButtonDown("Jump") && isGrounded)
    47.         {
    48.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    49.         }
    50.  
    51.         velocity.y += gravity * Time.deltaTime;
    52.  
    53.         controller.Move(velocity * Time.deltaTime);
    54.        }
    55.  
    56.     }
    57.  
    58.     }