-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathPlayerMotion.cs
More file actions
100 lines (86 loc) · 3 KB
/
PlayerMotion.cs
File metadata and controls
100 lines (86 loc) · 3 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Unity.Netcode.Components;
using UnityEngine;
#if UNITY_EDITOR
using Unity.Netcode.Editor;
using UnityEditor;
/// <summary>
/// The custom editor for the <see cref="MoverScript"/> component.
/// </summary>
[CustomEditor(typeof(PlayerMotion), true)]
public class PlayerMotionEditor : NetworkTransformEditor
{
private SerializedProperty m_Radius;
private SerializedProperty m_Speed;
public override void OnEnable()
{
m_Radius = serializedObject.FindProperty(nameof(PlayerMotion.Radius));
m_Speed = serializedObject.FindProperty(nameof(PlayerMotion.Speed));
base.OnEnable();
}
public override void OnInspectorGUI()
{
var playerMotion = target as PlayerMotion;
playerMotion.ExpandPlayerMotionProperties = EditorGUILayout.BeginFoldoutHeaderGroup(playerMotion.ExpandPlayerMotionProperties, $"{nameof(PlayerMotion)} Properties");
if (playerMotion.ExpandPlayerMotionProperties)
{
EditorGUILayout.EndFoldoutHeaderGroup();
EditorGUILayout.PropertyField(m_Radius);
EditorGUILayout.PropertyField(m_Speed);
}
else
{
EditorGUILayout.EndFoldoutHeaderGroup();
}
EditorGUILayout.Space();
playerMotion.ExpandNetworkTranspormProperties = EditorGUILayout.BeginFoldoutHeaderGroup(playerMotion.ExpandNetworkTranspormProperties, $"{nameof(NetworkTransform)} Properties");
if (playerMotion.ExpandNetworkTranspormProperties)
{
base.OnInspectorGUI();
}
else
{
serializedObject.ApplyModifiedProperties();
}
}
}
#endif
public class PlayerMotion : NetworkTransform
{
#if UNITY_EDITOR
public bool ExpandPlayerMotionProperties;
public bool ExpandNetworkTranspormProperties;
#endif
[Range(1.0f, 20.0f)]
public float Radius = 10.0f;
[Range(1.0f, 30.0f)]
public float Speed = 5.0f;
private float m_CurrentPi;
private float m_Increment = 0.25f;
private float m_ClockWise = 1.0f;
private Rigidbody m_RigidBody;
public override void OnNetworkSpawn()
{
// Always invoked base when deriving from NetworkTransform
base.OnNetworkSpawn();
m_RigidBody = GetComponent<Rigidbody>();
if (CanCommitToTransform)
{
m_CurrentPi = Random.Range(-Mathf.PI, Mathf.PI);
m_ClockWise = Random.Range(-1.0f, 1.0f);
m_ClockWise = m_ClockWise / Mathf.Abs(m_ClockWise);
if (!IsOwner)
{
Radius += Random.Range(-2.0f, 2.0f);
}
}
}
private void FixedUpdate()
{
if (IsSpawned && CanCommitToTransform)
{
m_CurrentPi += m_ClockWise * (Speed * m_Increment * Time.fixedDeltaTime);
var offset = new Vector3(Radius * Mathf.Cos(m_CurrentPi), transform.position.y, Radius * Mathf.Sin(m_CurrentPi));
m_RigidBody.MovePosition(Vector3.Lerp(transform.position, offset, Speed * 0.1f * Time.fixedDeltaTime));
}
}
}