Lag









How do I use Lag
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PickupCamera.cs Copy
94     void Apply( Transform dummyTarget, Vector3 dummyCenter )
95     {
96         // Early out if we don't have a target
97         if( !controller )
98             return;
99
100         Vector3 targetCenter = _target.position + centerOffset;
101         Vector3 targetHead = _target.position + headOffset;
102
103         // DebugDrawStuff();
104
105         // Calculate the current & target rotation angles
106         float originalTargetAngle = _target.eulerAngles.y;
107         float currentAngle = cameraTransform.eulerAngles.y;
108
109         // Adjust real target angle when camera is locked
110         float targetAngle = originalTargetAngle;
111
112         // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
113         // It will stop snapping when it reaches the target
114         if( Input.GetButton( "Fire2" ) )
115             snap = true;
116
117         if( snap )
118         {
119             // We are close to the target, so we can stop snapping now!
120             if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
121                 snap = false;
122
123             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
124         }
125         // Normal camera motion
126         else
127         {
128             if( controller.GetLockCameraTimer() < lockCameraTimeout )
129             {
130                 targetAngle = currentAngle;
131             }
132
133             // Lock the camera when moving backwards!
134             // * It is really confusing to do 180 degree spins when turning around.
135             if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
136                 targetAngle += 180;
137
138             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
139         }
140
141
142         // When jumping don't move camera upwards but only down!
143         if( controller.IsJumping() )
144         {
145             // We'd be moving the camera upwards, do that only if it's really high
146             float newTargetHeight = targetCenter.y + height;
147             if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
148                 targetHeight = targetCenter.y + height;
149         }
150         // When walking always update the target height
151         else
152         {
153             targetHeight = targetCenter.y + height;
154         }
155
156         // Damp the height
157         float currentHeight = cameraTransform.position.y;
158         currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
159
160         // Convert the angle into a rotation, by which we then reposition the camera
161         Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
162
163         // Set the position of the camera on the x-z plane to:
164         // distance meters behind the target
165         cameraTransform.position = targetCenter;
166         cameraTransform.position += currentRotation * Vector3.back * distance;
167
168         // Set the height of the camera
169         cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
170
171         // Always look at the target
172         SetUpRotation( targetCenter, targetHead );
173     }
File name: PickupCamera.cs Copy
180     void Cut( Transform dummyTarget, Vector3 dummyCenter )
181     {
182         float oldHeightSmooth = heightSmoothLag;
183         float oldSnapMaxSpeed = snapMaxSpeed;
184         float oldSnapSmooth = snapSmoothLag;
185
186         snapMaxSpeed = 10000;
187         snapSmoothLag = 0.001f;
188         heightSmoothLag = 0.001f;
189
190         snap = true;
191         Apply( transform, Vector3.zero );
192
193         heightSmoothLag = oldHeightSmooth;
194         snapMaxSpeed = oldSnapMaxSpeed;
195         snapSmoothLag = oldSnapSmooth;
196     }
File name: PickupController.cs Copy
151     void Update()
152     {
153         if (isControllable)
154         {
155             if (Input.GetButtonDown("Jump"))
156             {
157                 lastJumpButtonTime = Time.time;
158             }
159
160             UpdateSmoothedMovementDirection();
161
162             // Apply gravity
163             // - extra power jump modifies gravity
164             // - controlledDescent mode modifies gravity
165             ApplyGravity();
166
167             // Apply jumping logic
168             ApplyJumping();
169
170
171             // Calculate actual motion
172             Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
173             movement *= Time.deltaTime;
174
175             //Debug.Log(movement.x.ToString("0.000") + ":" + movement.z.ToString("0.000"));
176
177             // Move the controller
178             CharacterController controller = GetComponent();
179             collisionFlags = controller.Move(movement);
180
181         }
182
183         // PUN: if a remote position is known, we smooth-move to it (being late(r) but smoother)
184         if (this.remotePosition != Vector3.zero)
185         {
186             transform.position = Vector3.Lerp(transform.position, this.remotePosition, Time.deltaTime * this.RemoteSmoothing);
187         }
188
189         velocity = (transform.position - lastPos)*25;
190
191         // ANIMATION sector
192         if (_animation)
193         {
194             if (_characterState == PickupCharacterState.Jumping)
195             {
196                 if (!jumpingReachedApex)
197                 {
198                     _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
199                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
200                     _animation.CrossFade(jumpPoseAnimation.name);
201                 }
202                 else
203                 {
204                     _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
205                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
206                     _animation.CrossFade(jumpPoseAnimation.name);
207                 }
208             }
209             else
210             {
211                 if (_characterState == PickupCharacterState.Idle)
212                 {
213                     _animation.CrossFade(idleAnimation.name);
214                 }
215                 else if (_characterState == PickupCharacterState.Running)
216                 {
217                     _animation[runAnimation.name].speed = runMaxAnimationSpeed;
218                     if (this.isControllable)
219                     {
220                         _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
221                     }
222                     _animation.CrossFade(runAnimation.name);
223                 }
224                 else if (_characterState == PickupCharacterState.Trotting)
225                 {
226                     _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
227                     if (this.isControllable)
228                     {
229                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
230                     }
231                     _animation.CrossFade(walkAnimation.name);
232                 }
233                 else if (_characterState == PickupCharacterState.Walking)
234                 {
235                     _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
236                     if (this.isControllable)
237                     {
238                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
239                     }
240                     _animation.CrossFade(walkAnimation.name);
241                 }
242
243                 if (_characterState != PickupCharacterState.Running)
244                 {
245                     _animation[runAnimation.name].time = 0.0f;
246                 }
247             }
248         }
249         // ANIMATION sector
250
251         // Set rotation to the move direction
252         if (IsGrounded())
253         {
254             // a specialty of this controller: you can disable rotation!
255             if (DoRotate)
256             {
257                 transform.rotation = Quaternion.LookRotation(moveDirection);
258             }
259         }
260         else
261         {
262             /* This causes choppy behaviour when colliding with SIDES
263              * Vector3 xzMove = velocity;
264             xzMove.y = 0;
265             if (xzMove.sqrMagnitude > 0.001f)
266             {
267                 transform.rotation = Quaternion.LookRotation(xzMove);
268             }*/
269         }
270
271         // We are in jump mode but just became grounded
272         if (IsGrounded())
273         {
274             lastGroundedTime = Time.time;
275             inAirVelocity = Vector3.zero;
276             if (jumping)
277             {
278                 jumping = false;
279                 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
280             }
281         }
282
283         lastPos = transform.position;
284     }
File name: ThirdPersonCamera.cs Copy
86     void Apply( Transform dummyTarget, Vector3 dummyCenter )
87     {
88         // Early out if we don't have a target
89         if( !controller )
90             return;
91
92         Vector3 targetCenter = _target.position + centerOffset;
93         Vector3 targetHead = _target.position + headOffset;
94
95         // DebugDrawStuff();
96
97         // Calculate the current & target rotation angles
98         float originalTargetAngle = _target.eulerAngles.y;
99         float currentAngle = cameraTransform.eulerAngles.y;
100
101         // Adjust real target angle when camera is locked
102         float targetAngle = originalTargetAngle;
103
104         // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
105         // It will stop snapping when it reaches the target
106         if( Input.GetButton( "Fire2" ) )
107             snap = true;
108
109         if( snap )
110         {
111             // We are close to the target, so we can stop snapping now!
112             if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
113                 snap = false;
114
115             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
116         }
117         // Normal camera motion
118         else
119         {
120             if( controller.GetLockCameraTimer() < lockCameraTimeout )
121             {
122                 targetAngle = currentAngle;
123             }
124
125             // Lock the camera when moving backwards!
126             // * It is really confusing to do 180 degree spins when turning around.
127             if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
128                 targetAngle += 180;
129
130             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
131         }
132
133
134         // When jumping don't move camera upwards but only down!
135         if( controller.IsJumping() )
136         {
137             // We'd be moving the camera upwards, do that only if it's really high
138             float newTargetHeight = targetCenter.y + height;
139             if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
140                 targetHeight = targetCenter.y + height;
141         }
142         // When walking always update the target height
143         else
144         {
145             targetHeight = targetCenter.y + height;
146         }
147
148         // Damp the height
149         float currentHeight = cameraTransform.position.y;
150         currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
151
152         // Convert the angle into a rotation, by which we then reposition the camera
153         Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
154
155         // Set the position of the camera on the x-z plane to:
156         // distance meters behind the target
157         cameraTransform.position = targetCenter;
158         cameraTransform.position += currentRotation * Vector3.back * distance;
159
160         // Set the height of the camera
161         cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
162
163         // Always look at the target
164         SetUpRotation( targetCenter, targetHead );
165     }
File name: ThirdPersonCamera.cs Copy
172     void Cut( Transform dummyTarget, Vector3 dummyCenter )
173     {
174         float oldHeightSmooth = heightSmoothLag;
175         float oldSnapMaxSpeed = snapMaxSpeed;
176         float oldSnapSmooth = snapSmoothLag;
177
178         snapMaxSpeed = 10000;
179         snapSmoothLag = 0.001f;
180         heightSmoothLag = 0.001f;
181
182         snap = true;
183         Apply( transform, Vector3.zero );
184
185         heightSmoothLag = oldHeightSmooth;
186         snapMaxSpeed = oldSnapMaxSpeed;
187         snapSmoothLag = oldSnapSmooth;
188     }
File name: ThirdPersonController.cs Copy
436     public bool IsGrounded()
437     {
438         return (collisionFlags & CollisionFlags.CollidedBelow) != 0;
439     }
File name: PhotonEditor.cs Copy
909     public static void UpdateRpcList()
910     {
911         List additionalRpcs = new List();
912         HashSet currentRpcs = new HashSet();
913
914         var types = GetAllSubTypesInScripts(typeof(MonoBehaviour));
915
916         foreach (var mono in types)
917         {
918             MethodInfo[] methods = mono.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
919
920             foreach (MethodInfo method in methods)
921             {
922                 if (method.IsDefined(typeof(UnityEngine.RPC), false))
923                 {
924                     currentRpcs.Add(method.Name);
925
926                     if (!additionalRpcs.Contains(method.Name) && !PhotonEditor.Current.RpcList.Contains(method.Name))
927                     {
928                         additionalRpcs.Add(method.Name);
929                     }
930                 }
931             }
932         }
933
934         if (additionalRpcs.Count > 0)
935         {
936             // LIMITS RPC COUNT
937             if (additionalRpcs.Count + PhotonEditor.Current.RpcList.Count >= byte.MaxValue)
938             {
939                 if (currentRpcs.Count <= byte.MaxValue)
940                 {
941                     bool clearList = EditorUtility.DisplayDialog(CurrentLang.IncorrectRPCListTitle, CurrentLang.IncorrectRPCListLabel, CurrentLang.RemoveOutdatedRPCsLabel, CurrentLang.CancelButton);
942                     if (clearList)
943                     {
944                         PhotonEditor.Current.RpcList.Clear();
945                         PhotonEditor.Current.RpcList.AddRange(currentRpcs);
946                     }
947                     else
948                     {
949                         return;
950                     }
951                 }
952                 else
953                 {
954                     EditorUtility.DisplayDialog(CurrentLang.FullRPCListTitle, CurrentLang.FullRPCListLabel, CurrentLang.SkipRPCListUpdateLabel);
955                     return;
956                 }
957             }
958
959             additionalRpcs.Sort();
960             PhotonEditor.Current.RpcList.AddRange(additionalRpcs);
961             EditorUtility.SetDirty(PhotonEditor.Current);
962         }
963     }
File name: ReorderableListResources.cs Copy
147         public static Texture2D CreatePixelTexture( string name, Color color )
148         {
149             var tex = new Texture2D( 1, 1, TextureFormat.ARGB32, false, true );
150             tex.name = name;
151             tex.hideFlags = HideFlags.HideAndDontSave;
152             tex.filterMode = FilterMode.Point;
153             tex.SetPixel( 0, 0, color );
154             tex.Apply();
155             return tex;
156         }
File name: ReorderableListResources.cs Copy
168         private static void LoadResourceAssets()
169         {
170             var skin = EditorGUIUtility.isProSkin ? s_DarkSkin : s_LightSkin;
171             s_Cached = new Texture2D[ skin.Length ];
172
173             for( int i = 0; i < s_Cached.Length; ++i )
174             {
175                 // Get image data (PNG) from base64 encoded strings.
176                 byte[] imageData = Convert.FromBase64String( skin[ i ] );
177
178                 // Gather image size from image data.
179                 int texWidth, texHeight;
180                 GetImageSize( imageData, out texWidth, out texHeight );
181
182                 // Generate texture asset.
183                 var tex = new Texture2D( texWidth, texHeight, TextureFormat.ARGB32, false, true );
184                 tex.hideFlags = HideFlags.HideAndDontSave;
185                 tex.name = "(Generated) ReorderableList:" + i;
186                 tex.filterMode = FilterMode.Point;
187                 tex.LoadImage( imageData );
188
189                 s_Cached[ i ] = tex;
190             }
191
192             s_LightSkin = null;
193             s_DarkSkin = null;
194         }
File name: PhotonLagSimulationGui.cs Copy
57     private void NetSimWindow(int windowId)
58     {
59         GUILayout.Label(string.Format("Rtt:{0,4} +/-{1,3}", this.Peer.RoundTripTime, this.Peer.RoundTripTimeVariance));
60
61         bool simEnabled = this.Peer.IsSimulationEnabled;
62         bool newSimEnabled = GUILayout.Toggle(simEnabled, "Simulate");
63         if (newSimEnabled != simEnabled)
64         {
65             this.Peer.IsSimulationEnabled = newSimEnabled;
66         }
67
68         float inOutLag = this.Peer.NetworkSimulationSettings.IncomingLag;
69         GUILayout.Label("Lag " + inOutLag);
70         inOutLag = GUILayout.HorizontalSlider(inOutLag, 0, 500);
71
72         this.Peer.NetworkSimulationSettings.IncomingLag = (int)inOutLag;
73         this.Peer.NetworkSimulationSettings.OutgoingLag = (int)inOutLag;
74
75         float inOutJitter = this.Peer.NetworkSimulationSettings.IncomingJitter;
76         GUILayout.Label("Jit " + inOutJitter);
77         inOutJitter = GUILayout.HorizontalSlider(inOutJitter, 0, 100);
78
79         this.Peer.NetworkSimulationSettings.IncomingJitter = (int)inOutJitter;
80         this.Peer.NetworkSimulationSettings.OutgoingJitter = (int)inOutJitter;
81
82         float loss = this.Peer.NetworkSimulationSettings.IncomingLossPercentage;
83         GUILayout.Label("Loss " + loss);
84         loss = GUILayout.HorizontalSlider(loss, 0, 10);
85
86         this.Peer.NetworkSimulationSettings.IncomingLossPercentage = (int)loss;
87         this.Peer.NetworkSimulationSettings.OutgoingLossPercentage = (int)loss;
88
89         // if anything was clicked, the height of this window is likely changed. reduce it to be layouted again next frame
90         if (GUI.changed)
91         {
92             this.WindowRect.height = 100;
93         }
94
95         GUI.DragWindow();
96     }

Download file with original file name:Lag

Lag 127 lượt xem

Gõ tìm kiếm nhanh...