-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubeLerpDemo.cs
More file actions
64 lines (54 loc) · 2.29 KB
/
CubeLerpDemo.cs
File metadata and controls
64 lines (54 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using Unity.Netcode.Components;
public class CubeLerpDemo : MonoBehaviour {
[SerializeField] private NetworkTransform _netCubePrefab;
[SerializeField] private Transform _cubeStartPosAnchor;
[SerializeField] private Transform _cubeEndPosAnchor;
[SerializeField] private float _animDuration;
private NetworkTransform _netCube;
private Coroutine _prevLerper;
void Start() {
enabled = false;
if (NetworkManager.Singleton.IsServer)
OnServerStarted();
else
NetworkManager.Singleton.OnServerStarted += OnServerStarted;
}
private void OnServerStarted() {
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
_netCube = Instantiate(_netCubePrefab, _cubeStartPosAnchor.position, _cubeStartPosAnchor.rotation);
_netCube.NetworkObject.Spawn();
enabled = true;
}
private void OnGUI() {
if (GUI.Button(new Rect(10, 70, 300, 20),"Lerp Cube")) {
if (_prevLerper != null)
StopCoroutine(_prevLerper);
_prevLerper = StartCoroutine(LerpCube(_animDuration));
}
}
private IEnumerator LerpCube(float duration) {
Rigidbody netcubeRB = _netCube.GetComponent<Rigidbody>();
_cubeStartPosAnchor.rotation = Random.rotationUniform;
_netCube.transform.SetPositionAndRotation(_cubeStartPosAnchor.position, _cubeStartPosAnchor.rotation);
Vector3 torque = Random.insideUnitSphere * 10f;//new Vector3(7.90f, -0.10f, 0.00f);//
netcubeRB.isKinematic = false;
netcubeRB.AddTorque(torque, ForceMode.VelocityChange);
//Debug.Log(torque);
yield return new WaitForSeconds(1f);
_cubeStartPosAnchor.rotation = _netCube.transform.rotation;
float t = 0;
while (t < 1) {
t = Mathf.Clamp01(t + Time.deltaTime / duration);
netcubeRB.isKinematic = true;
_netCube.transform.SetPositionAndRotation(
Vector3.Lerp(_cubeStartPosAnchor.position, _cubeEndPosAnchor.position, t),
Quaternion.Euler(Vector3.Lerp(_cubeStartPosAnchor.eulerAngles, _cubeEndPosAnchor.eulerAngles, t))
);
yield return null;
}
}
}