Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Rotation Not Translating Between Clients Photon Pun

Discussion in 'Multiplayer' started by masterbanjo1234, May 10, 2023.

  1. masterbanjo1234

    masterbanjo1234

    Joined:
    Dec 2, 2022
    Posts:
    2
    Hi, I am making a multiplayer FPS game and I have hit a roadblock. I am unable to figure out why I can't get the rotation to translate. I have been following a tutorial but I used my own FPS controller, which is resulting in this issue. I am unsure of what I need to provide for someone to tell me what I have done wrong but I will provide a few things u might need.


    This is the FPS controller:

    Code (CSharp):
    1. using Photon.Pun;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     float playerHeight = 2f;
    9.  
    10.     [Header("References")]
    11.     [SerializeField] WallRun wallRun;
    12.  
    13.     [SerializeField] private float sensX = 80;
    14.     [SerializeField] private float sensY = 80;
    15.  
    16.     [SerializeField] Transform cam;
    17.     [SerializeField] Transform orientation;
    18.  
    19.     float mouseX;
    20.     float mouseY;
    21.  
    22.     float multiplier = 0.01f;
    23.  
    24.     float xRotation;
    25.     float yRotation;
    26.  
    27.  
    28.     [Header("Movement")]
    29.     [SerializeField] float movementSpeed = 6f;
    30.     [SerializeField] float airMultiplier = 0.4f;
    31.     float movementMultiplier = 10f;
    32.  
    33.     [Header("Sprinting")]
    34.     [SerializeField] float walkSpeed = 4f;
    35.     [SerializeField] float sprintSpeed = 10f;
    36.     [SerializeField] float acceleration = 10f;
    37.     [SerializeField] public bool sprint = false;
    38.  
    39.     [Header("Jumping")]
    40.     public float jumpForce = 5f;
    41.  
    42.     [Header("KeyBinds")]
    43.     [SerializeField] KeyCode jumpKey = KeyCode.Space;
    44.     [SerializeField] KeyCode sprintKey = KeyCode.LeftShift;
    45.  
    46.     [Header("Drag")]
    47.     public float groundDrag = 6f;
    48.     public float airDrag = 1f;
    49.  
    50.     float horizontalMovement;
    51.     float verticalMovement;
    52.  
    53.     [Header("Ground Detection")]
    54.     [SerializeField] Transform groundCheck;
    55.     [SerializeField] LayerMask groundMask;
    56.  
    57.     public bool isGrounded;
    58.     public bool wall;
    59.     float groundDistance = 0.7f;
    60.  
    61.     [Header("Scripts")]
    62.     [SerializeField] Dash dash;
    63.  
    64.     public bool canDoubleJump = true;
    65.  
    66.     Vector3 movementDirection;
    67.     Vector3 slopeMoveDirection;
    68.  
    69.     Rigidbody rb;
    70.     PhotonView PV;
    71.  
    72.     RaycastHit slopeHit;
    73.  
    74.     private bool onSlope()
    75.     {
    76.         if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
    77.         {
    78.             if(slopeHit.normal != Vector3.up)
    79.             {
    80.                 return true;
    81.             }
    82.             else
    83.             {
    84.                 return false;
    85.             }
    86.         }
    87.         return false;
    88.     }
    89.  
    90.     private void Start()
    91.     {
    92.         rb = GetComponent<Rigidbody>();
    93.         rb.freezeRotation = true;
    94.  
    95.         PV = GetComponent<PhotonView>();
    96.  
    97.         Cursor.lockState = CursorLockMode.Locked;
    98.         Cursor.visible = false;
    99.     }
    100.  
    101.     private void Update()
    102.     {
    103.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    104.  
    105.         MyInput();
    106.         ControlDrag();
    107.         ControlSpeed();
    108.         Jump();
    109.         Look();
    110.  
    111.         slopeMoveDirection = Vector3.ProjectOnPlane(movementDirection, slopeHit.normal);
    112.     }
    113.  
    114.     void MyInput()
    115.     {
    116.         horizontalMovement = Input.GetAxisRaw("Horizontal");
    117.         verticalMovement = Input.GetAxisRaw("Vertical");
    118.  
    119.         movementDirection = orientation.transform.forward * verticalMovement + orientation.transform.right * horizontalMovement;
    120.     }
    121.  
    122.     void Look()
    123.     {
    124.         if (PV.IsMine)
    125.         {
    126.             mouseX = Input.GetAxisRaw("Mouse X");
    127.             mouseY = Input.GetAxisRaw("Mouse Y");
    128.  
    129.             yRotation += mouseX * sensX * multiplier;
    130.             xRotation -= mouseY * sensY * multiplier;
    131.  
    132.             xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    133.  
    134.             cam.transform.localRotation = Quaternion.Euler(xRotation, 0, wallRun.tilt);
    135.             orientation.transform.rotation = Quaternion.Euler(0, yRotation, 0);
    136.         }
    137.     }
    138.  
    139.     void Jump()
    140.     {
    141.         if (isGrounded || canDoubleJump)
    142.         {
    143.             if (Input.GetKeyDown(jumpKey))
    144.             {
    145.                 rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
    146.                 rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    147.                 canDoubleJump = !canDoubleJump;
    148.             }
    149.         }
    150.     }
    151.  
    152.     void ControlSpeed()
    153.     {
    154.         if(Input.GetKeyDown(sprintKey))
    155.         {
    156.             sprint = !sprint;
    157.         }
    158.  
    159.         if (sprint)
    160.         {
    161.             movementSpeed = Mathf.Lerp(movementSpeed, sprintSpeed, acceleration * Time.deltaTime);
    162.         }
    163.         else
    164.         {
    165.             movementSpeed = Mathf.Lerp(movementSpeed, walkSpeed, acceleration * Time.deltaTime);
    166.         }
    167.     }
    168.  
    169.     void ControlDrag()
    170.     {
    171.         if(rb != null)
    172.         {
    173.             if (isGrounded)
    174.             {
    175.                 rb.drag = groundDrag;
    176.             }
    177.             else
    178.             {
    179.                 rb.drag = airDrag;
    180.             }
    181.         }  
    182.     }
    183.  
    184.     private void FixedUpdate()
    185.     {
    186.         MovePlayer();
    187.     }
    188.  
    189.     void MovePlayer()
    190.     {
    191.         if(rb != null)
    192.         {
    193.             if (isGrounded && !onSlope())
    194.             {
    195.                 rb.AddForce(movementDirection.normalized * movementSpeed * movementMultiplier, ForceMode.Acceleration);
    196.             }
    197.             else if (isGrounded && onSlope())
    198.             {
    199.                 rb.AddForce(slopeMoveDirection.normalized * movementSpeed * movementMultiplier, ForceMode.Acceleration);
    200.             }
    201.             else if (!isGrounded)
    202.             {
    203.                 rb.AddForce(movementDirection.normalized * movementSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
    204.             }
    205.         }
    206.     }
    207. }
    208.  
    This is my player prefab:

    upload_2023-5-10_17-57-27.png
     
  2. masterbanjo1234

    masterbanjo1234

    Joined:
    Dec 2, 2022
    Posts:
    2
    By the way when I say get the rotation to translate I mean get it to appear on the other clients screen