Unity and Photon: Scaling transforms of gameobjects in for loop causing jitter

Options

The gameobjects in the array (SequenceItems) have at least 1 child, and in some cases 1 child and 1 grandchild. the scaling works but it is very jittery when there are two players in the room. I am not sure if this is due to local scale being problematic, but in my research is does not seem to be an issue.

Here is my code:

 [PunRPC]
void ResizeCube(int index)
{

    for (int i = 0; i < SequenceItems.Length; i++)
    {
        if (i == index && myPV.IsMine )
            PhotonView.Find(SequenceItems[i].GetComponent<PhotonView>().ViewID).gameObject.transform.localScale = new Vector3(1f, 1.5f, 1f);

        else
            PhotonView.Find(SequenceItems[i].GetComponent<PhotonView>().ViewID).gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
    }
}


Comments

  • Tobias
    Options

    I can't see any smoothing here. So any change is applied whenever the RPC arrives.

    RPCs are always reliable, which is not the preferred way to send frequent updates.

    If you continuously update the scale, better do this via a script you observe, e.g. the PhotonTransformView.

  • verace
    Options

    I actually switched to swapping out the mesh, which I found more agreeable, I am still having the same issues. Is it possible you could look and see if there is anything that really stands out in my script? I am very stuck :( Thank you and I appreciate all feedback - I am new to photon

    using Microsoft.MixedReality.Toolkit.UI;
    using Photon.Pun;
    using System;
    using System.Collections;
    using UnityEngine;
    using UnityEngine.Events;
    using TMPro;
    
    
    
    
    public class PCSequencer : MonoBehaviour
    {
        public UnityEvent BigExplosionEvent;
        public bool play = true;
        public TMPro.TextMeshPro text;
      
        [Header("Sequence Controller")]
        private PhotonView myPV;
        public Mesh on;
        public Mesh off;
        [Tooltip("Drag in a bunch of GameObject you want in the sequence (must be in correct order before dragging in).")]
        /// <summary>
        /// List of GameObjects in sequence (must be in correct order)
        /// </summary>
        public GameObject[] SequenceItems;
        [Tooltip("The amount of time in seconds between sequence items.")]
        /// <summary>
        /// The interval of time in seconds between sequences.
        /// </summary>
        public float sequenceIntervalDelay = 0f;
        private int _currentIndex;
        [SerializeField]
        private TextMeshPro textMesh = null;
        /// <summary>
        /// The current sequence index.
        /// </summary>
        /// 
    
    
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {
                if (value != _currentIndex)
                {
                    // Ensure current index isn't more/less than the index bounds of the given Sequence Items...
                    if (_currentIndex >= 0 || _currentIndex <= SequenceItems.Length - 1)
                    {
                       
                        _currentIndex = value;
                        textMesh.text = _currentIndex.ToString();
                    }
                }
            }
        }
        /// <summary>
        /// Get things ready before first update occurs.
        /// </summary>
        void Start()
        {
            myPV = GetComponent<PhotonView>();
            if (BigExplosionEvent == null)
                BigExplosionEvent = new UnityEvent();
    
    
            var targetInfo = UnityEvent.GetValidMethodInfo(this, nameof(ExplodeMe), new Type[0]);
            UnityAction methodDelegate = Delegate.CreateDelegate(typeof(UnityAction), this, targetInfo) as UnityAction;
            ResetSequence();
            myPV.RPC("OnPlay", RpcTarget.AllBuffered);
    
    
        }
    
    
        [PunRPC]
        public void PlayPause_RPC()
        {
            play = !play;
    
    
            if (play)
            {
                text.text = "PAUSE";
            }
            else
            {
                text.text = "PLAY";
            }
        }
    
    
    
    
        public void ExplodeMe()
        {
            myPV.RPC("PlayPause_RPC", RpcTarget.AllBuffered);
    
    
        }
    
    
        [PunRPC]
        /// <summary>
        /// On play button pressed.
        /// </summary>
        private void OnPlay()
        {
            Debug.Log("on play");
            // Reset sequence back to start...
            if (CurrentIndex == SequenceItems.Length - 1)
            {
                ResetSequence();
            }
    
    
            // When Play button press, repeat call the OnNext() method...
            StartCoroutine("func");
          
            Debug.Log("Sequence Item " + CurrentIndex);
        }
    
    
    
    
        IEnumerator func()
        {
            while (true)
            {
                OnNext();
               
                yield return new WaitForSecondsRealtime(sequenceIntervalDelay); //Wait 1 second
                
            }
        }
    
    
        private void OnNext()
        {
           if (play)
            {
                if (myPV.IsMine)
                {
                    myPV.RPC("ResizeCube", RpcTarget.AllBuffered, CurrentIndex);
                }
                if (CurrentIndex < SequenceItems.Length)
                {
                    //   Debug.Log("what the index:  " + CurrentIndex);
    
    
                 
                 
                    if (PhotonView.Find(SequenceItems[CurrentIndex].GetComponent<PhotonView>().ViewID).gameObject.GetComponent<pcInteraction>().isActiveToPlay == true)
                    {
                       // Debug.Log("PlayNOte:  " + CurrentIndex);
                        SerialCommunication.sendNote(PhotonView.Find(SequenceItems[CurrentIndex].GetComponent<PhotonView>().ViewID).GetComponent<pcInteraction>().allCheckInsideSphere.currentNote);
                       // Debug.Log("sending note to arduino from sequencer");
    
    
                    }
                    else if (SequenceItems[CurrentIndex].GetComponent<pcInteraction>().isActiveToPlay == false)
                    {
    
    
                    }
                    CurrentIndex++;
                }
                else
                {
                    ResetSequence();
                }
            }
        }
        [PunRPC]
       void SeqSpeed_RPC(float updatedSpeed)
        {
            sequenceIntervalDelay = updatedSpeed;
        }
    
    
    
    
        public void OnSliderUpdated(SliderEventData eventData)
        {
    
    
            myPV.RPC("SeqSpeed_RPC", RpcTarget.AllBuffered, float.Parse($"{eventData.NewValue:F2}"));
    
    
        }
    
    
       [PunRPC]
        void ResizeCube(int index)
        {
            {
                for (int i = 0; i < SequenceItems.Length; i++)
                {
                    if (i == index)
                        //PhotonView.Find(SequenceItems[CurrentIndex].GetComponent<PhotonView>().ViewID).GetComponent<MeshFilter>().mesh = on;
                    SequenceItems[i].GetComponent<MeshFilter>().mesh = on;
                    else
                        //PhotonView.Find(SequenceItems[CurrentIndex].GetComponent<PhotonView>().ViewID).GetComponent<MeshFilter>().mesh = off;
                    SequenceItems[i].GetComponent<MeshFilter>().mesh = off;
                    
                }
            }
        }
    
    
        /// <summary>
        /// Resests the sequence back to the beginning.
        /// </summary>
        private void ResetSequence()
        {
            foreach (var go in SequenceItems)
            {
                // go.SetActive(false);
              //  go.GetComponent<MeshRenderer>().material.color = Color.white;
            }
           // SequenceItems.First().SetActive(true);
            CurrentIndex = 0;
        }
    }
    


  • verace
    verace
    edited September 2022
    Options

    Would Something like this work?

      
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
    
    
            if (stream.IsWriting)
            {
                stream.SendNext(CurrentIndex);
                //Debug.Log("Master " +CurrentIndex);
            }
    
    
    
    
            else
            {
                CurrentIndex = (int)stream.ReceiveNext();
              //  Debug.Log("Player " + CurrentIndex);
                myPV.RPC("ResizeCube", RpcTarget.AllBuffered, CurrentIndex);
                textMesh.text = _currentIndex.ToString();
            }
        }
    
  • verace
    Options

    Still doesn't work - do I need to make the data (CurrentIndex) smoother? When there is only one player in the room the data is smooth, as soon as another player enters, the data becomes jittery...