Local
How do I use Local
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: JumpAndRunMovement.cs
Copy
40 void UpdateFacingDirection()
41 {
42 if( m_Body.velocity.x > 0.2f )
43 {
44 transform.localScale = new Vector3( 1, 1, 1 );
45 }
46 else if( m_Body.velocity.x < -0.2f )
47 {
48 transform.localScale = new Vector3( -1, 1, 1 );
49 }
50 }
File name: DemoMecanimGUI.cs
Copy
16 private PhotonAnimatorView m_AnimatorView; // local animatorView. set when we create our character in CreatePlayerObject()
17 private Animator m_RemoteAnimator; // to display the synchronized values on the right side in the GUI. A third player will simply be ignored (until the second player leaves)
28 public void Awake()
29 {
30
31 }
File name: RPGCamera.cs
Copy
20 void Start()
21 {
22 m_CameraTransform = transform.GetChild( 0 );
23 m_LocalForwardVector = m_CameraTransform.forward;
24
25 m_Distance = -m_CameraTransform.localPosition.z / m_CameraTransform.forward.z;
26 m_Distance = Mathf.Clamp( m_Distance, MinimumDistance, MaximumDistance );
27 m_LookAtPoint = m_CameraTransform.localPosition + m_LocalForwardVector * m_Distance;
28 }
File name: RPGCamera.cs
Copy
43 void UpdateZoom()
44 {
45 m_CameraTransform.localPosition = m_LookAtPoint - m_LocalForwardVector * m_Distance;
46 }
File name: CubeExtra.cs
Copy
21 // this method is called by PUN when this script is being "observed" by a PhotonView (setup in inspector)
22 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
23 {
24 // Always send transform (depending on reliability of the network view)
25 if (stream.isWriting)
26 {
27 Vector3 pos = transform.localPosition;
28 Quaternion rot = transform.localRotation;
29 stream.Serialize(ref pos);
30 stream.Serialize(ref rot);
31 }
32 // When receiving, buffer the information
33 else
34 {
35 // Receive latest state information
36 Vector3 pos = Vector3.zero;
37 Quaternion rot = Quaternion.identity;
38 stream.Serialize(ref pos);
39 stream.Serialize(ref rot);
40
41 lastMovement = (pos - latestCorrectPos) / (Time.time - lastTime);
42
43 lastTime = Time.time;
44 latestCorrectPos = pos;
45
46 transform.position = latestCorrectPos;
47 }
48 }
File name: CubeExtra.cs
Copy
50 // This only runs where the component is enabled, which is only on remote peers (server/clients)
51 public void Update()
52 {
53 transform.localPosition += lastMovement * Time.deltaTime;
54 }
File name: CubeInter.cs
Copy
34 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
35 {
36 // Always send transform (depending on reliability of the network view)
37 if (stream.isWriting)
38 {
39 Vector3 pos = transform.localPosition;
40 Quaternion rot = transform.localRotation;
41 stream.Serialize(ref pos);
42 stream.Serialize(ref rot);
43 }
44 // When receiving, buffer the information
45 else
46 {
47 // Receive latest state information
48 Vector3 pos = Vector3.zero;
49 Quaternion rot = Quaternion.identity;
50 stream.Serialize(ref pos);
51 stream.Serialize(ref rot);
52
53 // Shift buffer contents, oldest data erased, 18 becomes 19, ... , 0 becomes 1
54 for (int i = m_BufferedState.Length - 1; i >= 1; i--)
55 {
56 m_BufferedState[i] = m_BufferedState[i - 1];
57 }
58
59
60 // Save currect received state as 0 in the buffer, safe to overwrite after shifting
61 State state;
62 state.timestamp = info.timestamp;
63 state.pos = pos;
64 state.rot = rot;
65 m_BufferedState[0] = state;
66
67 // Increment state count but never exceed buffer size
68 m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);
69
70 // Check integrity, lowest numbered state in the buffer is newest and so on
71 for (int i = 0; i < m_TimestampCount - 1; i++)
72 {
73 if (m_BufferedState[i].timestamp < m_BufferedState[i + 1].timestamp)
74 Debug.Log("State inconsistent");
75 }
76 }
77 }
File name: CubeInter.cs
Copy
79 // This only runs where the component is enabled, which is only on remote peers (server/clients)
80 void Update()
81 {
82 double currentTime = PhotonNetwork.time;
83 double interpolationTime = currentTime - interpolationBackTime;
84 // We have a window of interpolationBackTime where we basically play
85 // By having interpolationBackTime the average ping, you will usually use interpolation.
86 // And only if no more data arrives we will use extrapolation
87
88 // Use interpolation
89 // Check if latest state exceeds interpolation time, if this is the case then
90 // it is too old and extrapolation should be used
91 if (m_BufferedState[0].timestamp > interpolationTime)
92 {
93 for (int i = 0; i < m_TimestampCount; i++)
94 {
95 // Find the state which matches the interpolation time (time+0.1) or use last state
96 if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount - 1)
97 {
98 // The state one slot newer (<100ms) than the best playback state
99 State rhs = m_BufferedState[Mathf.Max(i - 1, 0)];
100 // The best playback state (closest to 100 ms old (default time))
101 State lhs = m_BufferedState[i];
102
103 // Use the time between the two slots to determine if interpolation is necessary
104 double length = rhs.timestamp - lhs.timestamp;
105 float t = 0.0F;
106 // As the time difference gets closer to 100 ms t gets closer to 1 in
107 // which case rhs is only used
108 if (length > 0.0001)
109 t = (float)((interpolationTime - lhs.timestamp) / length);
110
111 // if t=0 => lhs is used directly
112 transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
113 transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
114 return;
115 }
116 }
117 }
118 // Use extrapolation. Here we do something really simple and just repeat the last
119 // received state. You can do clever stuff with predicting what should happen.
120 else
121 {
122 State latest = m_BufferedState[0];
123
124 transform.localPosition = Vector3.Lerp(transform.localPosition, latest.pos, Time.deltaTime * 20 );
125 transform.localRotation = latest.rot;
126 }
127 }
File name: CubeLerp.cs
Copy
35 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
36 {
37 if (stream.isWriting)
38 {
39 Vector3 pos = transform.localPosition;
40 Quaternion rot = transform.localRotation;
41 stream.Serialize(ref pos);
42 stream.Serialize(ref rot);
43 }
44 else
45 {
46 // Receive latest state information
47 Vector3 pos = Vector3.zero;
48 Quaternion rot = Quaternion.identity;
49
50 stream.Serialize(ref pos);
51 stream.Serialize(ref rot);
52
53 latestCorrectPos = pos; // save this to move towards it in FixedUpdate()
54 onUpdatePos = transform.localPosition; // we interpolate from here to latestCorrectPos
55 fraction = 0; // reset the fraction we alreay moved. see Update()
56
57 transform.localRotation = rot; // this sample doesn't smooth rotation
58 }
59 }
File name: CubeLerp.cs
Copy
61 public void Update()
62 {
63 // We get 10 updates per sec. sometimes a few less or one or two more, depending on variation of lag.
64 // Due to that we want to reach the correct position in a little over 100ms. This way, we usually avoid a stop.
65 // Lerp() gets a fraction value between 0 and 1. This is how far we went from A to B.
66 //
67 // Our fraction variable would reach 1 in 100ms if we multiply deltaTime by 10.
68 // We want it to take a bit longer, so we multiply with 9 instead.
69
70 fraction = fraction + Time.deltaTime * 9;
71 transform.localPosition = Vector3.Lerp(onUpdatePos, latestCorrectPos, fraction); // set our pos between A and B
72 }
Local 145 lượt xem
Gõ tìm kiếm nhanh...