Search Unity

Third Party Help with photonview.IsMine

Discussion in 'Multiplayer' started by Mertcan_uff, Jan 23, 2023.

  1. Mertcan_uff

    Mertcan_uff

    Joined:
    Feb 3, 2022
    Posts:
    2
    Can someone help me
    please I really tried every fricking thing I could but i won't work




    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Photon.Pun;
    6. public class Movement : MonoBehaviourPunCallbacks
    7. {
    8.     public GunSystem[] allGuns;
    9.     private int selectedGun;
    10.     public Camera myCam;
    11.    
    12.     [SerializeField] Transform playerCamera;
    13.     [SerializeField][Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
    14.     [SerializeField] bool cursorLock = true;
    15.     public float mouseSensitivity, mouseSensitivityAfterAim = 3.5f;
    16.     public float mouseSensitivityInAim = 1f;
    17.     [SerializeField] float Speed = 6.0f;
    18.     [SerializeField] float runSpeed = 6.0f;
    19.     [SerializeField][Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
    20.     [SerializeField] float gravity = -30f;
    21.     [SerializeField] Transform groundCheck;
    22.     [SerializeField] LayerMask ground;
    23.     public float jumpHeight = 6f;
    24.     public float jumpPadHeight;
    25.     float velocityY;
    26.     bool isGrounded;
    27.     float cameraCap;
    28.     Vector2 currentMouseDelta;
    29.     Vector2 currentMouseDeltaVelocity;
    30.    
    31.     CharacterController controller;
    32.     Vector2 currentDir;
    33.     Vector2 currentDirVelocity;
    34.     Vector3 velocity;
    35.     public float sprintFOV;
    36.     public float aimFOV;
    37.     public float normalFOV;
    38.     public float FOVChangeSpeed;
    39.  
    40.  
    41.  
    42.     private float verticalRotStore;
    43.  
    44.     void Start()
    45.     {
    46.         if(photonView.IsMine)
    47.         {
    48.             controller = GetComponent<CharacterController>();
    49.             SwitchGun();
    50.             if (cursorLock)
    51.             {
    52.                 Cursor.lockState = CursorLockMode.Locked;
    53.                 Cursor.visible = false;
    54.                
    55.             }
    56.  
    57.             normalFOV = myCam.fieldOfView;
    58.             sprintFOV = myCam.fieldOfView += 45;
    59.         }
    60.  
    61.         //Transform newTrans = SpawnManager.instance.GetSpawnPoint();
    62.         //transform.position = newTrans.position;
    63.         //transform.rotation = newTrans.rotation;
    64.     }
    65.     void Update()
    66.     {
    67.        
    68.             UpdateMouse();
    69.             UpdateMove();
    70.            
    71.  
    72.             if(Input.GetKeyDown(KeyCode.Escape))
    73.             {
    74.                 Cursor.lockState = CursorLockMode.None;
    75.             }
    76.  
    77.             if(Input.GetAxisRaw("Mouse ScrollWheel") > 0)
    78.             {
    79.                 selectedGun++;
    80.                 if(selectedGun >= allGuns.Length)
    81.                 {
    82.                     selectedGun = 0;
    83.                 }
    84.                 SwitchGun();
    85.             }
    86.             else if(Input.GetAxisRaw("Mouse ScrollWheel") < 0)
    87.             {
    88.                 selectedGun--;
    89.  
    90.                 if(selectedGun < 0)
    91.                 {
    92.                     selectedGun = allGuns.Length - 1;
    93.                 }
    94.                 SwitchGun();
    95.             }
    96.  
    97.             for(int i = 0; i < allGuns.Length; i++)
    98.             {
    99.                 if(Input.GetKeyDown((i + 1).ToString()))
    100.                 {
    101.                         selectedGun = i;
    102.                         SwitchGun();
    103.                 }
    104.             }
    105.        
    106.      
    107.  
    108.      
    109.      
    110.     }
    111.  
    112.     public void SwitchGun()
    113.     {
    114.         foreach(GunSystem gun in allGuns)
    115.         {
    116.             gun.gameObject.SetActive(false);
    117.         }
    118.  
    119.         allGuns[selectedGun].gameObject.SetActive(true);
    120.     }
    121.  
    122.      void OnTriggerEnter(Collider collider)
    123.     {
    124.         if(collider.gameObject.tag == "JumpPad")
    125.         {
    126.             Debug.Log("Niasnd");
    127.             velocityY = Mathf.Sqrt(jumpPadHeight * -2f * gravity);
    128.         }
    129.     }
    130.     void UpdateMouse()
    131.     {
    132.         if(photonView.IsMine)
    133.         {
    134.             Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
    135.    
    136.             currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
    137.    
    138.             cameraCap -= currentMouseDelta.y * mouseSensitivity;
    139.    
    140.             cameraCap = Mathf.Clamp(cameraCap, -80.0f, 80.0f);
    141.    
    142.             playerCamera.localEulerAngles = Vector3.right * cameraCap;
    143.    
    144.             transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
    145.         }
    146.        
    147.     }
    148.  
    149.  
    150.     void UpdateMove()
    151.     {
    152.         if(photonView.IsMine)
    153.         {
    154.             isGrounded = Physics.CheckSphere(groundCheck.position, 0.2f, ground);
    155.    
    156.             Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    157.             targetDir.Normalize();
    158.    
    159.             currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
    160.    
    161.             velocityY += gravity * 2f * Time.deltaTime;
    162.    
    163.             Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * Speed + Vector3.up * velocityY;
    164.    
    165.             controller.Move(velocity * Time.deltaTime);
    166.    
    167.             if (isGrounded && Input.GetButtonDown("Jump"))
    168.             {
    169.                 velocityY = Mathf.Sqrt(jumpHeight * -2f * gravity);
    170.             }
    171.    
    172.             if(isGrounded! && controller.velocity.y < -1f)
    173.             {
    174.                 velocityY = -8f;
    175.             }
    176.  
    177.             if(Input.GetKey(KeyCode.LeftShift))
    178.             {
    179.                 Speed = runSpeed;
    180.                 myCam.fieldOfView = Mathf.Lerp(myCam.fieldOfView, sprintFOV, FOVChangeSpeed);
    181.             }
    182.             else
    183.             {
    184.                 Speed = 12;
    185.                 myCam.fieldOfView = Mathf.Lerp(myCam.fieldOfView, normalFOV, FOVChangeSpeed);
    186.             }
    187.         }
    188.     }
    189.  
    190. }
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    To help us help you in any way: What is it supposed to do and what happens instead?
     
  3. Mertcan_uff

    Mertcan_uff

    Joined:
    Feb 3, 2022
    Posts:
    2
    Online FPS

    I Want that the player controls the player he / her is playing in the built version

    When I start the game and join with a build version, Everyone is playing every spawn point of the map.
    As I said, I tried using photonView.isMine, but When I Start it with that, the game goes crazy it lags, it stutters, the weapon is glitching etc..
    I think I tried any variation and I dont know what to do right now..

    Thanks for offering your help!

    https://github.com/Mertcanuff/ProjectShare
    Here is the Project if it helps!