Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Photon Transform View Not Syncing Sometimes

Discussion in 'Multiplayer' started by Mashimaro7, Jun 29, 2021.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    I'm not sure what's going on, I have a photon transform view on both my model and the "holder" of the model. It was working just fine, until I swapped out models? Idk I've been working on this for 10s of hours so I might've changed something in code, but it WAS working fine before.

    I have an animation that sits down in a chair. So, the weird thing is the position is off, but only on the player who sits' screen, and not the other player's screen. Here's a screenshot,

    https://imgur.com/a/yYfJT7V

    It was working on both before. I don't understand it, but either way this would indicate that the Transform View is not syncing as it should, as the position is displaying differently on both screens. The model is at 0,0,0. And this is the exact same animation I was using before, so it's not some issue with the animation(I also have animator views on the armatures, set to continuous on all parameters)

    Here's my code, the sit down method is at 212.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Google.XR.Cardboard;
    5. using Photon.Pun;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     Camera cam;
    10.     public Transform model;
    11.     Transform holder,cameraAnchor;
    12.  
    13.     Vector3 playerOffset;
    14.     [SerializeField] Vector3 sittingOffset;
    15.     Vector3 preSitPos, preSitParentPos, cameraAnchorPos;
    16.  
    17.     [SerializeField] float targetSpeed, toggleAngle = 30, sitSpeed, smoothSpeed = 1;
    18.     float speedGoal, speed, defaultY;
    19.     CharacterController controller;
    20.     [SerializeField] float camMaxX, camMinX;
    21.  
    22.     [SerializeField] GameObject emoteUI,playerUI;
    23.  
    24.     public PlayerEmotions emotes;
    25.  
    26.     [HideInInspector]public Animator anim;
    27.  
    28.     public Transform chair;
    29.     public float timerToStand,timerToTalk;
    30.     float animationSpeedGoal, animationSmoothSpeed;
    31.  
    32.     public bool sitting,standing,canWalk;
    33.     PhotonView view;
    34.  
    35.     float yPos,rotX;
    36.  
    37.     RaycastHit hit;
    38.  
    39.     public PlayerData info;
    40.  
    41.     [SerializeField]LayerMask rayLayerToIgnore;
    42.  
    43.     [SerializeField] TextManager speechBubble;
    44.  
    45.     bool talking;
    46.  
    47.     // Start is called before the first frame update
    48.     void Start()
    49.     {
    50.      
    51.         SetDependencies();
    52.         emoteUI.SetActive(false);
    53.  
    54.  
    55.         cameraAnchor = transform.parent.transform.Find("CameraAnchor");
    56.         cameraAnchorPos = cameraAnchor.localPosition;
    57.  
    58.         Ray ray = new Ray(holder.position, Vector3.down);
    59.      
    60.         if (Physics.Raycast(ray,out hit,100,~rayLayerToIgnore))
    61.         {
    62.             yPos = hit.point.y + holder.position.y;
    63.         }
    64.     }
    65.  
    66.     private void SetDependencies()
    67.     {
    68.  
    69.         view = GetComponent<PhotonView>();
    70.         cam = GetComponent<Camera>();
    71.  
    72.         if (!view.IsMine)
    73.         {
    74.             cam.enabled = false;
    75.             Destroy(GetComponent<AudioListener>());
    76.             playerUI.SetActive(false);
    77.         }
    78.  
    79.         emoteUI = transform.parent.transform.Find("PlayerUI").gameObject;
    80.  
    81.         holder = transform.parent;
    82.         controller = holder.GetComponent<CharacterController>();
    83.  
    84.         StartCoroutine(Delay());
    85.     }
    86.  
    87.     IEnumerator Delay()
    88.     {
    89.  
    90.         yield return new WaitForSeconds(0.5f);
    91.         model = transform.parent.GetComponentInChildren<Identifier>().transform;
    92.             anim = model.GetComponentInChildren<Animator>();
    93.  
    94.             emotes = model.GetComponentInChildren<PlayerEmotions>();
    95.  
    96.             playerOffset = model.localPosition;
    97.  
    98.             cameraAnchor.parent = model;
    99.      
    100.     }
    101.     private void Update()
    102.     {
    103.         if(timerToStand > 0)
    104.         {
    105.             timerToStand -= Time.deltaTime;
    106.         }
    107.         else if(sitting && standing)
    108.         {
    109.             sitting = false;
    110.        
    111.             chair = null;
    112.  
    113.             anim.SetBool("Standing", false);
    114.         }
    115.      
    116.         if(timerToTalk > 0)
    117.         {
    118.             timerToTalk -= Time.deltaTime;
    119.         }
    120.         else if (talking)
    121.         {
    122.             talking = false;
    123.          
    124.             anim.SetBool("Talking", talking);
    125.         }
    126.  
    127.     }
    128.  
    129.     // Update is called once per frame
    130.     void FixedUpdate()
    131.     {
    132.  
    133.         if (Mathf.Abs(Vector3.Dot(transform.forward, Vector3.down)) > toggleAngle && !sitting && canWalk)
    134.         {
    135.  
    136.             if (view.IsMine) MovePlayer();
    137.          
    138.         }
    139.         else
    140.         {
    141.             animationSpeedGoal = 0;
    142.             speedGoal = 0;
    143.         }
    144.         if(animationSmoothSpeed != animationSpeedGoal)
    145.         {
    146.             animationSmoothSpeed = Mathf.Lerp(animationSmoothSpeed, animationSpeedGoal, 3 * Time.deltaTime);
    147.         }
    148.  
    149.      
    150.         if (!sitting && transform.eulerAngles.x < 89)
    151.         {
    152.  
    153.             if (view.IsMine)
    154.             {
    155.                 RotatePlayer();
    156.                 Ray ray = new Ray(holder.position, Vector3.down);
    157.  
    158.                 if (Physics.Raycast(ray, out hit, 100, ~rayLayerToIgnore))
    159.                 {
    160.                     if (holder.position.y != yPos && !sitting)
    161.                     {
    162.                         holder.position = Vector3.Lerp(holder.position, new Vector3(holder.position.x, yPos, holder.position.z), smoothSpeed * Time.deltaTime);
    163.                     }
    164.                 }
    165.             }
    166.             if (chair != null)
    167.             {
    168.                 chair.gameObject.SetActive(false);
    169.             }
    170.         }
    171.  
    172.         transform.position = cameraAnchor.position;
    173.  
    174.        /* if(Application.platform == RuntimePlatform.WindowsEditor)
    175.         {
    176.             float mouseY = Input.GetAxis("Mouse Y");
    177.             float mouseX = Input.GetAxis("Mouse X");
    178.  
    179.             rotX -= mouseY;
    180.  
    181.             rotX = Mathf.Clamp(rotX, -85f, 85f);
    182.             //rotX *= Time.deltaTime * mouseSens;
    183.             transform.localEulerAngles = new Vector3(rotX, transform.localEulerAngles.y, transform.localEulerAngles.z);
    184.             transform.Rotate(Vector3.up * mouseX);
    185.         }*/
    186.     }
    187.  
    188.     void RotatePlayer()
    189.     {
    190.  
    191.         if(anim != null)anim.SetFloat("Speed", animationSmoothSpeed);
    192.         if(model != null)model.transform.localEulerAngles = new Vector3(model.transform.localEulerAngles.x, transform.localEulerAngles.y, model.localEulerAngles.z);
    193.     }
    194.  
    195.     void MovePlayer()
    196.     {
    197.         if (talking)
    198.         {
    199.             talking = false;
    200.             anim.SetBool("Talking", talking);
    201.             timerToTalk = 0;
    202.         }
    203.         speed = Mathf.Lerp(speed, speedGoal, smoothSpeed * Time.deltaTime);
    204.         Vector3 direction = new Vector3(transform.forward.x, 0, transform.forward.z).normalized;
    205.         controller.Move(direction * speed * Time.deltaTime);
    206.  
    207.         speedGoal = targetSpeed;
    208.         animationSpeedGoal = 1;
    209.  
    210.     }
    211.  
    212.     public void SitDown(Transform c, Vector3 pos)
    213.     {
    214.         if (c.parent.GetComponentInChildren<ChairSit>().someoneSitting) return;
    215.         if (!view.IsMine) return;
    216.  
    217.         standing = false;
    218.         if (timerToStand <= 0 && !sitting)
    219.         {
    220.             cameraAnchor.position = new Vector3(cameraAnchor.position.x, cameraAnchor.position.y - 1, cameraAnchor.position.z);
    221.             anim.SetBool("Sitting", true);
    222.             anim.Play("Sit");
    223.             chair = c;
    224.             sitting = true;
    225.             timerToStand = 2;
    226.  
    227.             defaultY = holder.position.y;
    228.             holder.position = c.position + sittingOffset;
    229.             print(c.position);
    230.             model.localRotation = Quaternion.Euler(Vector3.zero);
    231.             transform.localRotation = model.localRotation;
    232.             holder.transform.rotation = chair.rotation;
    233.  
    234.             emoteUI.SetActive(true);
    235.         }
    236.     }
    237.     public void StandUp()
    238.     {
    239.         if (!view.IsMine) return;
    240.         if (timerToStand <= 0)
    241.         {
    242.             anim.SetBool("Sitting", false);
    243.             anim.SetBool("Standing", true);
    244.             holder.localScale = Vector3.one;
    245.             chair.parent.GetComponentInChildren<ChairSit>().StandUp();
    246.             chair.gameObject.SetActive(true);
    247.  
    248.             standing = true;
    249.             holder.parent = null;
    250.          
    251.             timerToStand = 1.25f;
    252.  
    253.             //p.position += new Vector3(chair.forward.x, 0, chair.forward.z)/2;
    254.  
    255.             cameraAnchor.localPosition = cameraAnchorPos;
    256.  
    257.             emoteUI.SetActive(false);
    258.         }
    259.     }
    260.  
    261.     public void TalkAnimation()
    262.     {
    263.         talking = true;
    264.         anim.SetBool("Talking", talking);
    265.         timerToTalk = 1;
    266.     }
    267.  
    268. }
    269.  

    Apologies for my super messy code. Since it's not super obvious, holder is the player holder, model is the player model inside the holder, and this script is attached to the camera, so any "transform.position"s are moving the camera. Also probably not super important, but this is a VR game for mobile, and I have tested it with editor and mobile, one or the other sitting, the result is always the same, the one doing the sitting clips through the chair, but the one not sitting sees them sitting perfectly fine.

    I'm really at a loss, I don't know what else I can do, I've tried setting the sitter of the chair as the parent and setting it to vector3.zero but that has the same result. I don't understand why it's position is syncing normally except when you sit down...

    Thanks.

    Edit: Strangely after HOURS of trying to fix this, I finally fixed it. I think it was fixed by adding a transform view to my camera? Not exactly sure why that fixed it, but okay haha. Doubt this is a help to anyone, I'd delete it if I could.
     
    Last edited: Jun 29, 2021