Dong_Minh_Doxygen: D:/Google Drive/Unity Projects/Tank Game/Assets/Scripts/PlayerController.cs Source File

Dong Minh

Dong_Minh_Doxygen
PlayerController.cs
Go to the documentation of this file.
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.SceneManagement;
5 
6 public class PlayerController : MonoBehaviour {
7 
8  // ------------ \\
9  // Game Objects \\
10  // ------------ \\
11  GameObject Turret;
12  GameObject Cannon;
13  GameObject[] FrontWheels;
14  GameObject[] BackWheels;
15  GameObject[] FrontTurningWheels;
16  public Transform SteeringLeft;
17  public Transform SteeringRight;
18 
19  // --------------------------------- \\
20  // Public Variables for use in Unity \\
21  // --------------------------------- \\
22  public float TankSpeed; // how fast the tank will move
23  public float RotationSpeed; // how fast the tank can rotate
24  public float CannonSpeed; // how fast the cannon can rotate
25  public float FrontWheelRotation; // how fast the front wheels rotate
26  public float BackWheelRotation; // how fast the back wheels rotate
27  public float horizontal;
28 
34  void Start (){
35  Turret = GameObject.FindGameObjectWithTag ("TurretObject");
36  Cannon = GameObject.FindGameObjectWithTag ("CannonObject");
37  FrontWheels = GameObject.FindGameObjectsWithTag ("FrontWheelsObject");
38  BackWheels = GameObject.FindGameObjectsWithTag ("BackWheelsObject");
39  FrontTurningWheels = GameObject.FindGameObjectsWithTag ("FrontWheelsTurningObject");
40  } // end start
41 
47  void Update () {
48  // stuff that works
49  HorizontalWheel ();
50  WheelSteering ();
53  CannonRotation ();
54  TurretRotation ();
55  TankMovement ();
56  if (Input.GetKeyDown (KeyCode.R)) {
57  SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
58  }
59  } // end void update
60 
61 
62  // ----------------------------- \\
63  // Wheel MOVEMENT SECTION \\
64  // ----------------------------- \\
65 
79  // fill this later
80  if (Input.GetKey(KeyCode.D))
81  {
82  horizontal = Input.GetKey(KeyCode.D) ? 0.45f : 0;
83  }
84  else
85  horizontal = Input.GetKey(KeyCode.A) ? -0.45f : 0;
86  }
87 
96 
97  // For moving forward or backward
98  if (Input.GetKey (KeyCode.W)) {
99 
100  //Debug.Log ("W Key is pressed");
101 
102  var RotateForward = Time.deltaTime * FrontWheelRotation;
103 
104  FrontWheels[0].transform.Rotate(RotateForward, 0, 0);
105  FrontWheels[1].transform.Rotate(RotateForward, 0, 0);
106  } // end get key for W
107  else if (Input.GetKey (KeyCode.S)) {
108 
109  //Debug.Log ("S Key is pressed");
110 
111  var RotateForward = Time.deltaTime * FrontWheelRotation;
112 
113  FrontWheels[0].transform.Rotate(-RotateForward, 0, 0);
114  FrontWheels[1].transform.Rotate(-RotateForward, 0, 0);
115  } // end get key for S
116  } // end front wheel movement function
117 
126  float rot = 100;
127  float current_rot = rot * horizontal;
128 
129  Vector3 front_rotationAmount = transform.rotation.eulerAngles;
130  front_rotationAmount.y += current_rot;
131 
132  SteeringLeft.rotation = Quaternion.Euler(front_rotationAmount);
133  SteeringRight.rotation = Quaternion.Euler(front_rotationAmount);
134  }
135 
144  if (Input.GetKey (KeyCode.W)) {
145 
146  //Debug.Log ("W Key is pressed");
147 
148  var RotateBackward = Time.deltaTime * BackWheelRotation;
149 
150  BackWheels[0].transform.Rotate(RotateBackward, 0, 0);
151  BackWheels[1].transform.Rotate(RotateBackward, 0, 0);
152  } // end get key for W
153  else if (Input.GetKey (KeyCode.S)) {
154 
155  //Debug.Log ("S Key is pressed");
156 
157  var RotateBackward = Time.deltaTime * BackWheelRotation;
158 
159  BackWheels[0].transform.Rotate(-RotateBackward, 0, 0);
160  BackWheels[1].transform.Rotate(-RotateBackward, 0, 0);
161  } // end get key for S
162  }
163 
164 
165  // ----------------------------- \\
166  // CANNON MOVEMENT SECTION \\
167  // ----------------------------- \\
168 
177 
178  var tilt = Time.deltaTime * CannonSpeed;
179 
180  if (Input.GetKey (KeyCode.UpArrow)) {
181  Debug.Log ("Up Arrow Key Pressed");
182 
183  float currentRotationZ = Cannon.transform.rotation.z * 100;
184 
185  //Debug.Log (currentRotationZ);
186 
187  if (currentRotationZ <= -70){
188 
189  //Debug.Log ("Input Up key is being called for first if");
190 
191  currentRotationZ = -70 / 100;
192  }
193  else {
194  //Debug.Log ("Input Up key is being called for second if");
195 
196  currentRotationZ /= 100;
197  currentRotationZ -= tilt;
198  }
199 
200  Cannon.transform.Rotate (0, 0, currentRotationZ);
201  }
202  else if (Input.GetKey (KeyCode.DownArrow)) {
203  Debug.Log ("Down Arrow Key Pressed");
204 
205  float currentRotationZ = Cannon.transform.rotation.z * 100;
206 
207  //Debug.Log (currentRotationZ);
208 
209  if (currentRotationZ >= 0){
210 
211  //Debug.Log ("Input Down key is being called for first if");
212 
213  currentRotationZ = 0 / 100;
214  }
215  else{
216  //Debug.Log ("Input Down key is being called for second if");
217 
218  currentRotationZ /= 100;
219  currentRotationZ -= tilt;
220  }
221 
222  Cannon.transform.Rotate (0, 0, -currentRotationZ);
223  }
224  } // end cannon rotation function
225 
226  // ----------------------------- \\
227  // TURRET MOVEMENT SECTION \\
228  // ----------------------------- \\
229 
238 
239  if (Input.GetKey (KeyCode.LeftArrow)) {
240  Debug.Log ("Left Arrow Key Pressed");
241 
242  var rotateLeft = Time.deltaTime * RotationSpeed;
243 
244  Turret.transform.Rotate (0, -rotateLeft, 0);
245  //Cannon.transform.Rotate (0, -rotateLeft, 0);
246  }
247  else if (Input.GetKey (KeyCode.RightArrow)) {
248  Debug.Log ("Right Arrow Key Pressed");
249 
250  var rotateRight = Time.deltaTime * RotationSpeed;
251 
252  Turret.transform.Rotate (0, rotateRight, 0);
253  //Cannon.transform.Rotate (0, rotateRight, 0);
254  }
255 
256  } // end turret rotation function
257 
258  // ----------------------------- \\
259  // TANK MOVEMENT SECTION \\
260  // ----------------------------- \\
261 
269  private void TankMovement(){
270  // W or D keys
271  if (Input.GetKey (KeyCode.W)) {
272 
273  Debug.Log ("W Key is pressed");
274 
275  var MoveForward = Time.deltaTime * TankSpeed;
276 
277  ForwardBackward (-MoveForward);
278 
279  } // end get key for W
280  else if (Input.GetKey (KeyCode.S)) {
281 
282  Debug.Log ("S Key is pressed");
283 
284  var MoveBackward = Time.deltaTime * TankSpeed;
285 
286  ForwardBackward (MoveBackward);
287  }
288 
289  // A or D keys
290  if (Input.GetKey (KeyCode.A)) {
291 
292  Debug.Log ("A Key is pressed");
293 
294  var TurnLeft = Time.deltaTime * TankSpeed;
295 
296  LeftRight (-TurnLeft);
297  }
298  else if (Input.GetKey (KeyCode.D)) {
299 
300  Debug.Log ("D Key is pressed");
301 
302  var TurnRight = Time.deltaTime * TankSpeed;
303 
304  LeftRight (TurnRight);
305  }
306  }
307 
315  private void ForwardBackward (float movement){
316  transform.Translate (movement, 0, 0);
317  } // end ForwardBackward function
318 
326  private void LeftRight (float movement){
327  transform.Rotate (0, movement, 0);
328  } // end LeftRight function
329 }
void Start()
Initalize at the beginning of the game
void CannonRotation()
How far the cannon should rotate up and to its rest position. Max for up position is vertical (90 deg...
Transform SteeringRight
void WheelSteering()
The steering for left or right to determine how far it should also rotate.
void HorizontalWheel()
The front wheels will go either rotate left or right by 45 degrees.
GameObject [] BackWheels
void ForwardBackward(float movement)
When the W or S key is pressed, the tank will move forward or backward respectively ...
GameObject [] FrontTurningWheels
void LeftRight(float movement)
When the A or D key is pressed, the tank will move left or right respectively
Transform SteeringLeft
void Update()
Update is called once per frame
void FrontWheelMovement()
The Front wheels should rotate forward/backward and should rotate with an angle of maxiumum of 45 to ...
void BackWheelMovement()
The back wheels should rotate and also accelerate/decelerate the tank
GameObject [] FrontWheels
void TankMovement()
This is called in the update function. All funcitonality is done here for the tank movement like movi...
void TurretRotation()
Will rotate the turret left or right when the user press left or right arrow keys ...
Generated by   doxygen 1.8.13