/// <summary> /// Called when the local player object has been set up. /// /// This happens after OnStartClient(), as it is triggered by an ownership message from the server. /// This is an appropriate place to activate components or functionality that should only be active for /// the local player, such as cameras and input. /// </summary> public override void OnStartLocalPlayer() { //com.ootii.Utilities.Debug.Log.FileWrite(Time.time.ToString("f3") + " " + gameObject.name + "[" + gameObject.GetInstanceID() + "].OnStartLocalPlayer() isLocalPlayer:" + isLocalPlayer + " isServer:" + isServer); // Tell the local player that we want to auto send parameters #if UNITY_5_5 || UNITY_5_6 int lParamCount = (mAnimator != null ? mAnimator.parameterCount : 14); #else int lParamCount = 14; #endif NetworkAnimator lNetworkAnimator = gameObject.GetComponent <NetworkAnimator>(); if (lNetworkAnimator != null && lNetworkAnimator.enabled) { for (int i = 0; i < lParamCount; i++) { lNetworkAnimator.SetParameterAutoSend(i, true); } } // Initialize the params array for (int i = 0; i < mLocalState.Length; i++) { mLocalState[i] = new ActorNetworkState(); } }
/// <summary> /// Update function run on the local instance /// </summary> protected void LocalUpdate() { // Shift the array to the right ActorNetworkState lLastParam = mLocalState[mLocalState.Length - 1]; for (int i = mLocalState.Length - 1; i > 0; i--) { mLocalState[i] = mLocalState[i - 1]; } // Add the new param lLastParam.Time = Time.time; if (_SyncPosition) { lLastParam.Position = mTransform.position; } if (_SyncRotation) { lLastParam.Rotation = mTransform.rotation; } if (_SyncMotionPhase) { lLastParam.L0MotionPhase = mAnimator.GetInteger("L0MotionPhase"); lLastParam.L0MotionForm = mAnimator.GetInteger("L0MotionForm"); lLastParam.L1MotionPhase = mAnimator.GetInteger("L1MotionPhase"); lLastParam.L1MotionForm = mAnimator.GetInteger("L1MotionForm"); } if (_SyncAnimatorParams) { lLastParam.IsGrounded = mAnimator.GetBool("IsGrounded"); lLastParam.Stance = mAnimator.GetInteger("Stance"); lLastParam.InputX = mAnimator.GetFloat("InputX"); lLastParam.InputY = mAnimator.GetFloat("InputY"); lLastParam.InputMagnitude = mAnimator.GetFloat("InputMagnitude"); lLastParam.InputMagnitudeAvg = mAnimator.GetFloat("InputMagnitudeAvg"); lLastParam.InputAngleFromAvatar = mAnimator.GetFloat("InputAngleFromAvatar"); lLastParam.InputAngleFromCamera = mAnimator.GetFloat("InputAngleFromCamera"); lLastParam.L0MotionParameter = mAnimator.GetInteger("L0MotionParameter"); lLastParam.L0MotionStateTime = mAnimator.GetFloat("L0MotionStateTime"); lLastParam.L1MotionParameter = mAnimator.GetInteger("L1MotionParameter"); lLastParam.L1MotionStateTime = mAnimator.GetFloat("L1MotionStateTime"); } mLocalState[0] = lLastParam; // Determine if its time to send the update mSendElapsedTime = mSendElapsedTime + Time.deltaTime; if (mSendElapsedTime >= mSendDelay) { mSendElapsedTime = 0f; CmdUpdateActor(mLocalState); } }
/// <summary> /// Update function run on the remote instance /// </summary> protected void RemoteUpdate() { if (mRemoteState != null && mRemoteState.Length > 0) { int lStateIndex = 0; ActorNetworkState lNetworkState = mRemoteState[lStateIndex]; mPhaseElapsedTime = mPhaseElapsedTime + Time.deltaTime; if (_SyncPosition) { mTransform.position = Vector3.Lerp(mTransform.position, mRemoteState[lStateIndex].Position, _SyncFactor); } if (_SyncRotation) { mTransform.rotation = Quaternion.Lerp(mTransform.rotation, mRemoteState[lStateIndex].Rotation, _SyncFactor); } // Set the current animator parameters int lL0MotionPhase = mAnimator.GetInteger("L0MotionPhase"); int lL1MotionPhase = mAnimator.GetInteger("L1MotionPhase"); // Cycle through the states (oldest to newest) to find the first unprocessed state for (int i = mRemoteState.Length - 1; i >= 0; i--) { if (mRemoteState[i].Time <= mLastServerParamTime) { continue; } bool lStop = false; // If the value is different than our current parameter, set it if (mRemoteState[i].L0MotionPhase != lL0MotionPhase) { if (_PhaseChangeDelay <= 0f || mPhaseElapsedTime > _PhaseChangeDelay) { lStop = true; lNetworkState = mRemoteState[i]; mPhaseElapsedTime = 0f; if (_SyncMotionPhase) { mAnimator.SetInteger("L0MotionPhase", lNetworkState.L0MotionPhase); } } } // If the value is different than our current parameter, set it if (mRemoteState[i].L1MotionPhase != lL1MotionPhase) { lStop = true; if (_SyncMotionPhase) { mAnimator.SetInteger("L1MotionPhase", mRemoteState[i].L1MotionPhase); } } if (lStop) { mLastServerParamTime = mRemoteState[i].Time; break; } } if (_SyncAnimatorParams) { mAnimator.SetBool("IsGrounded", lNetworkState.IsGrounded); mAnimator.SetInteger("Stance", lNetworkState.Stance); mAnimator.SetFloat("InputX", lNetworkState.InputX); mAnimator.SetFloat("InputY", lNetworkState.InputY); mAnimator.SetFloat("InputMagnitude", lNetworkState.InputMagnitude); mAnimator.SetFloat("InputMagnitudeAvg", lNetworkState.InputMagnitudeAvg); mAnimator.SetFloat("InputAngleFromAvatar", lNetworkState.InputAngleFromAvatar); mAnimator.SetFloat("InputAngleFromCamera", lNetworkState.InputAngleFromCamera); mAnimator.SetInteger("L0MotionParameter", lNetworkState.L0MotionParameter); mAnimator.SetFloat("L0MotionStateTime", lNetworkState.L0MotionStateTime); mAnimator.SetInteger("L1MotionParameter", lNetworkState.L1MotionParameter); mAnimator.SetFloat("L1MotionStateTime", lNetworkState.L1MotionStateTime); } } }