Slerp









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

Featured Snippets


File name: MoveCam.cs Copy
22     private void Update()
23     {
24         camTransform.position = Vector3.Slerp(camTransform.position, randomPos, Time.deltaTime);
25         camTransform.LookAt(lookAt);
26         if (Vector3.Distance(camTransform.position, randomPos) < 0.5f)
27         {
28             randomPos = originalPos + new Vector3(Random.Range(-2, 2), Random.Range(-2, 2), Random.Range(-1, 1));
29         }
30     }
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: RotateCamera.cs Copy
48  protected void Update() {
49   if (!rotate) return;
50   lookAngleY += inputManager.MouseAxis.x * horizAngleMove;
51   newRotY = Quaternion.Euler(0f,lookAngleY,0f);
52
53   lookAngleX += inputManager.MouseAxis.y * vertAngleMove;
54   lookAngleX = Mathf.Clamp(lookAngleX, -xAngleMin, xAngleMax);
55   newRotX = Quaternion.Euler(lookAngleX, pivotEulers.y, pivotEulers.z);
56
57   if (turnSmoothing > 0) {
58    pivotTransform.localRotation = Quaternion.Slerp(pivotTransform.localRotation, newRotX, turnSmoothing * Time.deltaTime);
59    transform.localRotation = Quaternion.Slerp(transform.localRotation, newRotY, turnSmoothing * Time.deltaTime);
60   } else {
61    transform.localRotation = newRotY;
62    pivotTransform.localRotation = newRotX;
63   }
64  }
File name: ScrollSnap.cs Copy
52  void LateUpdate() {
53   if(isLerping) {
54    LerpToElement();
55    if(ShouldStopLerping()) {
56     isLerping = false;
57     canvasGroup.blocksRaycasts = true;
58     onLerpComplete.Invoke();
59     onLerpComplete.RemoveListener(WrapElementAround);
60    }
61   }
62  }
File name: ScrollSnap.cs Copy
164  void StartLerping() {
165   releasedPosition = content.anchoredPosition;
166   targetPosition = CalculateTargetPoisition(cellIndex);
167   lerpStartedAt = DateTime.Now;
168   canvasGroup.blocksRaycasts = false;
169   isLerping = true;
170  }

Slerp 120 lượt xem

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