-
Notifications
You must be signed in to change notification settings - Fork 0
/
TankMovement.cs
170 lines (134 loc) · 6.33 KB
/
TankMovement.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using UnityEngine;
using UnityEngine.Assertions;
namespace Complete
{
public class TankMovement : MonoBehaviour
{
public int m_PlayerNumber = 1; // Used to identify which tank belongs to which player. This is set by this tank's manager.
public float m_Speed = 12f; // How fast the tank moves forward and back.
public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
public AudioSource m_MovementAudio; // Reference to the audio source used to play engine sounds. NB: different to the shooting audio source.
public AudioClip m_EngineIdling; // Audio to play when the tank isn't moving.
public AudioClip m_EngineDriving; // Audio to play when the tank is moving.
public float m_PitchRange = 0.2f; // The amount by which the pitch of the engine noises can vary.
private string m_MovementAxisName; // The name of the input axis for moving forward and back.
private string m_TurnAxisName; // The name of the input axis for turning.
private Rigidbody m_Rigidbody; // Reference used to move the tank.
private float m_MovementInputValue; // The current value of the movement input.
private float m_TurnInputValue; // The current value of the turn input.
private float m_OriginalPitch; // The pitch of the audio source at the start of the scene.
// FPS variables
int m_frameCounter = 0;
float m_timeCounter = 0.0f;
float m_lastFramerate = 0.0f;
public float m_refreshTime = 0.1f;
// Tank Visiblity Counter variables
/*public TankManager tankManager;
public GameObject tank;
public Collider m_ObjCollider;
private Camera cam;
private Plane[] planes;*/
private void Awake ()
{
m_Rigidbody = GetComponent<Rigidbody> ();
}
private void OnEnable ()
{
// When the tank is turned on, make sure it's not kinematic.
m_Rigidbody.isKinematic = false;
// Also reset the input values.
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
private void OnDisable ()
{
// When the tank is turned off, set it to kinematic so it stops moving.
m_Rigidbody.isKinematic = true;
}
private void Start ()
{
// The axes names are based on player number.
m_MovementAxisName = "Vertical" + m_PlayerNumber;
m_TurnAxisName = "Horizontal" + m_PlayerNumber;
// Store the original pitch of the audio source.
m_OriginalPitch = m_MovementAudio.pitch;
/*cam = Camera.main;
planes = GeometryUtility.CalculateFrustumPlanes(cam);
m_ObjCollider = GetComponent<Collider>();*/
}
private void Update ()
{
// Store the value of both input axes.
m_MovementInputValue = Input.GetAxis (m_MovementAxisName);
m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
if (m_timeCounter < m_refreshTime)
{
m_timeCounter += Time.deltaTime;
m_frameCounter++;
}
else
{
m_lastFramerate = (float)m_frameCounter / m_timeCounter;
m_frameCounter = 0;
m_timeCounter = 0.0f;
}
// Debug.Log("FPS: " + m_lastFramerate);
// Assert.IsTrue(m_lastFramerate < 60, "The FPS is less than 60.");
/*tankManager = GameObject.Find("TankManager");
tank = tankManager.m_Instance;
if (GeometryUtility.TestPlanesAABB(planes, m_ObjCollider.bounds))
Debug.Log(m_Tank.name + "is still visible.");
else
Debug.Log("Tank is invisible");*/
EngineAudio ();
}
private void EngineAudio ()
{
// If there is no input (the tank is stationary)...
if (Mathf.Abs (m_MovementInputValue) < 0.1f && Mathf.Abs (m_TurnInputValue) < 0.1f)
{
// ... and if the audio source is currently playing the driving clip...
if (m_MovementAudio.clip == m_EngineDriving)
{
// ... change the clip to idling and play it.
m_MovementAudio.clip = m_EngineIdling;
m_MovementAudio.pitch = Random.Range (m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play ();
}
}
else
{
// Otherwise if the tank is moving and if the idling clip is currently playing...
if (m_MovementAudio.clip == m_EngineIdling)
{
// ... change the clip to driving and play.
m_MovementAudio.clip = m_EngineDriving;
m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play();
}
}
}
private void FixedUpdate ()
{
// Adjust the rigidbodies position and orientation in FixedUpdate.
Move ();
Turn ();
}
private void Move ()
{
// Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
// Apply this movement to the rigidbody's position.
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}
private void Turn ()
{
// Determine the number of degrees to be turned based on the input, speed and time between frames.
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
// Make this into a rotation in the y axis.
Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
// Apply this rotation to the rigidbody's rotation.
m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation);
}
}
}