ControllerScriptCS.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * FUNCTION:
  3. * - This script detects inputs (swipes and gyro) and controls the
  4. * player accordingly.
  5. * - It also defines the physics that defines
  6. * the user's actions.
  7. * - It is responsible for handling all player animations.
  8. * - It is responsible for process all collisions.
  9. * - It is responsible for initiating the death scene.
  10. *
  11. * USED BY: This script is a part of the "Player" prefab.
  12. */
  13. using UnityEngine;
  14. using System.Collections;
  15. public class ControllerScriptCS : MonoBehaviour
  16. {
  17. public static ControllerScriptCS Instance { get; private set; }
  18. public bool Accelerate;
  19. enum StrafeDirection {Strafe_Left = 0, Strafe_Right = 1}
  20. public enum ControlType { Swipe = 1, Gyro = 2 }
  21. private Transform tPlayer; //the main character transform
  22. private Transform tPlayerRotation; //Player child transform to rotate it in game
  23. private Animation aPlayer; //character animation
  24. private Animator aPlayerMecAnim;
  25. private bool mecanimEnabled=false;
  26. private Vector3 v3DefaultPlayerAnimPosition;
  27. private Vector3 v3DefaultPlayerAnimRotation;
  28. private Transform tPlayerSidesCollider; //sides collider transform (detects stumble)
  29. private Transform tFrontCollider; //front collider transfrom (detects collisions)
  30. private Vector3 v3BNCDefaultScale;
  31. private Vector3 v3BFCDefaultScale;
  32. //Variables
  33. private float fCurrentWalkSpeed;
  34. private float tCurrentAngle = 0.0f; //current rotation along Y axis
  35. private float fJumpForwardFactor = 0.0f; //movement speed increase on jump
  36. private float fCurrentUpwardVelocity = 0.0f; //speed during the duration of jump
  37. private float fCurrentHeight = 0.0f;
  38. private float fContactPointY = 0.0f; //y-axis location of the path
  39. //player state during gameplay
  40. private bool bInAir = false;
  41. private bool bJumpFlag = false;
  42. private bool bInJump = false;
  43. private bool bDiveFlag = false; //force character to dive during jump
  44. private bool bExecuteLand = false;
  45. private bool bInStrafe = false;
  46. private bool bInDuck = false; //true if the character is sliding
  47. private float fForwardAccleration = 0.0f;
  48. private Transform tBlobShadowPlane; //the shadow under the player
  49. private Vector3 CurrentDirection;//set player rotation according to path
  50. //script references
  51. private PatchesRandomizerCS hPatchesRandomizerCS;
  52. private CheckPointsMainCS hCheckPointsMainCS;
  53. private InGameScriptCS hInGameScriptCS;
  54. private PitsMainControllerCS hPitsMainControllerCS;
  55. private SoundManagerCS hSoundManagerCS;
  56. private CameraControllerCS hCameraControllerCS;
  57. private PowerupsMainControllerCS hPowerupScriptCS;
  58. private EnemyControllerCS hEnemyControllerCS;
  59. private MenuScriptCS hMenuScriptCS;
  60. private MissionsControllerCS hMissionsControllerCS;
  61. private GlobalAchievementControllerCS hGlobalAchievementControllerCS;
  62. private PlayerFrontColliderScriptCS hPlayerFrontColliderScriptCS;
  63. private PlayerSidesColliderScriptCS hPlayerSidesColliderScriptCS;
  64. private NGUIMenuScript hNGUIMenuScript;
  65. private RaycastHit hitInfo; //whats under the player character
  66. private bool bGroundhit = false; //is that an object under the player character
  67. public float fHorizontalDistance = 0.0f; //calculate player's horizontal distance on path
  68. private float fCurrentForwardSpeed = .5f; //sets movement based on spline
  69. private float fCurrentDistance = 0.0f;//distance between the start and current position during the run
  70. public float fCurrentMileage = 0.0f;//used to calculate the score based on distance covered
  71. //detect if there is a terrain_lyr under the player
  72. private float fPitFallLerpValue = 0.0f;
  73. private float fPitFallForwardSpeed = 0.0f;
  74. private float fPitPositionX = 0.0f;//check the position of the pit in x-axis
  75. private float fDeathAnimStartTime = 0;
  76. private int iDeathAnimEndTime = 2; //duration wait for death scene
  77. private bool JumpAnimationFirstTime = true; //play death animation once
  78. private Camera HUDCamera;
  79. private Transform tPauseButton;
  80. private Transform tHUDGroup;
  81. private SwipeControlsCS swipeLogic;
  82. public int iLanePosition; //current lane number -- -1, 0 or 1
  83. public int iLastLanePosition; //stores the previous lane on lane change
  84. private bool bMouseReleased = true;
  85. public bool bControlsEnabled = true;
  86. //action queue
  87. private SwipeControlsCS.SwipeDirection directionQueue;
  88. private bool bDirectionQueueFlag = false;
  89. //Physics Constants
  90. //change these to adjust the initial and final movement speed
  91. public float fStartingWalkSpeed = 220.0f;//when player starts running
  92. private float fEndingWalkSpeed = 500.0f; //final speed after acclerating
  93. private float fCurrentWalkAccleration = 0.5f; //rate of accleartion
  94. //change these to adjust the jump height and displacement
  95. private float fJumpPush = 185; //force with which player pushes the ground on jump
  96. private int getAccleration() { return 500; } //accleration and deceleration on jump
  97. //the initial distance of the player character at launch
  98. //from the start of the path
  99. private float fCurrentDistanceOnPath = 0.0f;
  100. //switch between gyro and swipe controls
  101. private bool swipeControlsEnabled = true;
  102. public bool isSwipeControlEnabled() { return swipeControlsEnabled; }
  103. public void toggleSwipeControls(bool state)
  104. {
  105. swipeControlsEnabled = state;
  106. //permanently save user preference of controls
  107. PlayerPrefs.SetInt("ControlsType", (state == true ? 1 : 0));
  108. PlayerPrefs.Save();
  109. }
  110. public void Start()
  111. {
  112. Instance = this;
  113. //script references
  114. hPatchesRandomizerCS = (PatchesRandomizerCS)this.GetComponent(typeof(PatchesRandomizerCS));
  115. hMissionsControllerCS = (MissionsControllerCS)this.GetComponent(typeof(MissionsControllerCS));
  116. hGlobalAchievementControllerCS = (GlobalAchievementControllerCS)this.GetComponent(typeof(GlobalAchievementControllerCS));
  117. hPlayerSidesColliderScriptCS = (PlayerSidesColliderScriptCS)GameObject.Find("PlayerSidesCollider").GetComponent(typeof(PlayerSidesColliderScriptCS));
  118. hPlayerFrontColliderScriptCS = (PlayerFrontColliderScriptCS)GameObject.Find("PlayerFrontCollider").GetComponent(typeof(PlayerFrontColliderScriptCS));
  119. hSoundManagerCS = (SoundManagerCS)GameObject.Find("SoundManager").GetComponent(typeof(SoundManagerCS));
  120. hInGameScriptCS = (InGameScriptCS)this.GetComponent(typeof(InGameScriptCS));
  121. hPitsMainControllerCS = (PitsMainControllerCS)this.GetComponent(typeof(PitsMainControllerCS));
  122. hCheckPointsMainCS = (CheckPointsMainCS)this.GetComponent(typeof(CheckPointsMainCS));
  123. hPowerupScriptCS = (PowerupsMainControllerCS)this.GetComponent(typeof(PowerupsMainControllerCS));
  124. hEnemyControllerCS = (EnemyControllerCS)GameObject.Find("Enemy").GetComponent(typeof(EnemyControllerCS));
  125. hPowerupScriptCS = (PowerupsMainControllerCS)this.GetComponent(typeof(PowerupsMainControllerCS));
  126. hCameraControllerCS = (CameraControllerCS)GameObject.Find("Main Camera").GetComponent(typeof(CameraControllerCS));
  127. swipeLogic = (SwipeControlsCS)transform.GetComponent(typeof(SwipeControlsCS));
  128. hNGUIMenuScript = (NGUIMenuScript)GameObject.Find("UI Root (2D)").GetComponent(typeof(NGUIMenuScript));
  129. //check which type of menu (Custom or NGUI) to work with
  130. if (hInGameScriptCS.isCustomMenuEnabled())
  131. {
  132. hMenuScriptCS = (MenuScriptCS)GameObject.Find("MenuGroup").GetComponent(typeof(MenuScriptCS));
  133. HUDCamera = GameObject.Find("HUDCamera").GetComponent<Camera>();
  134. tHUDGroup = GameObject.Find("HUDMainGroup/HUDGroup").transform;
  135. tPauseButton = GameObject.Find("HUDMainGroup/HUDGroup/HUDPause").transform;
  136. }
  137. tPlayer = transform;
  138. tPlayerRotation = transform.Find("PlayerRotation");
  139. //get the animation component of the player character
  140. if (this.transform.Find("PlayerRotation/PlayerMesh/SuperSledge"))
  141. {
  142. if (this.transform.Find("PlayerRotation/PlayerMesh/Prisoner"))
  143. {
  144. mecanimEnabled = false;
  145. aPlayer = (Animation)this.transform.Find("PlayerRotation/PlayerMesh/Prisoner").GetComponent(typeof(Animation));
  146. StartCoroutine("playIdleAnimations");//start playing idle animations
  147. }
  148. else if (this.transform.Find("PlayerRotation/PlayerMesh/Prisoner(MecAnim)"))//check for mecanim animated character
  149. {
  150. mecanimEnabled = true;
  151. aPlayerMecAnim = (Animator)this.transform.Find("PlayerRotation/PlayerMesh/Prisoner(MecAnim)").GetComponent(typeof(Animator));
  152. v3DefaultPlayerAnimPosition = aPlayerMecAnim.transform.localPosition;//get the default player position
  153. v3DefaultPlayerAnimRotation = aPlayerMecAnim.transform.localEulerAngles;//get the default player rotation
  154. }
  155. }
  156. else
  157. {
  158. }
  159. tBlobShadowPlane = transform.Find("BlobShadowPlane");//get the shadow
  160. tPlayerSidesCollider = GameObject.Find("PlayerSidesCollider").transform;//get the sides collider to detect stumbles
  161. tFrontCollider = GameObject.Find("PlayerFrontCollider").transform;//get the front collider to detect collisions
  162. v3BNCDefaultScale = tFrontCollider.localScale;
  163. v3BFCDefaultScale = tPlayerSidesCollider.localScale;
  164. bInAir = false;
  165. fCurrentDistanceOnPath = 50.0f; //inital distance with respect to spline
  166. fCurrentDistance = 0.0f;
  167. fCurrentMileage = 0.0f;
  168. tCurrentAngle = 0.0f;
  169. fPitFallLerpValue = 0.0f;
  170. fPitFallForwardSpeed = 0.0f;
  171. fPitPositionX = 0.0f;
  172. fDeathAnimStartTime = 0;
  173. bGroundhit = false;
  174. bJumpFlag = false;
  175. bInJump = false;
  176. fCurrentUpwardVelocity = 0;
  177. fCurrentHeight = 0;
  178. bDirectionQueueFlag = false;
  179. directionQueue = SwipeControlsCS.SwipeDirection.Null;
  180. iLanePosition = 0; //set current lane to mid
  181. fCurrentWalkSpeed = fStartingWalkSpeed;
  182. //get the type of controls (swipe or gyro) set by user
  183. if (PlayerPrefs.HasKey("ControlsType"))
  184. swipeControlsEnabled = PlayerPrefs.GetInt("ControlsType") == 1 ? true : false;
  185. else
  186. PlayerPrefs.SetInt("ControlsType", (swipeControlsEnabled == true ? 1 : 0));
  187. //stop footsteps sound if playing
  188. hSoundManagerCS.stopSound(SoundManagerCS.CharacterSounds.Footsteps);
  189. }//end of Start()
  190. /*
  191. * FUNCTION: Play and alternate between the two idle animations
  192. * when the game is launched/ restarted.
  193. * CALLED BY: Start()
  194. * */
  195. private IEnumerator playIdleAnimations()
  196. {
  197. while(true)
  198. {
  199. yield return new WaitForFixedUpdate();
  200. //check if idle animations are not being played
  201. if (!aPlayer.IsPlaying("Idle_1") && !aPlayer.IsPlaying("Idle_2"))
  202. {
  203. aPlayer.GetComponent<Animation>().Play("Idle_1");//play the idle animation
  204. //wait for the current animation to end and play the second idle animation
  205. aPlayer.PlayQueued("Idle_2", QueueMode.CompleteOthers);
  206. }
  207. }//end of while
  208. }
  209. /*
  210. * FUNCTION: Enable controls, start player animation and movement
  211. */
  212. public void launchGame()
  213. {
  214. StopCoroutine("playIdleAnimations");//stop idle animations
  215. hEnemyControllerCS.launchEnemy();
  216. if (!mecanimEnabled)//if legacy animations enabled
  217. {
  218. switch (hInGameScriptCS.Level)
  219. {
  220. case 1:
  221. //fStartingWalkSpeed = 400;
  222. //fCurrentWalkSpeed = fStartingWalkSpeed;
  223. break;
  224. case 2:
  225. break;
  226. case 3:
  227. fStartingWalkSpeed = 274;
  228. fCurrentWalkSpeed = fStartingWalkSpeed;
  229. break;
  230. }
  231. togglePlayerAnimation(true);
  232. aPlayer["run"].speed = Mathf.Clamp( (fCurrentWalkSpeed/fStartingWalkSpeed)/1.1f, 0.8f, 1.2f );
  233. aPlayer.Play("run");
  234. PlayerController.Instance.Run();
  235. }
  236. else//if mecanim enabled
  237. aPlayerMecAnim.SetBool("RunAnim", true);
  238. hSoundManagerCS.playSound(SoundManagerCS.CharacterSounds.Footsteps);//play the footsteps sound
  239. }
  240. void SwitchController()
  241. {
  242. }
  243. void Update()
  244. {
  245. if(hInGameScriptCS.isGamePaused()==true)
  246. return;
  247. if (hInGameScriptCS.isEnergyZero())
  248. if(DeathScene())
  249. return;
  250. if (hInGameScriptCS.isCustomMenuEnabled())
  251. getClicks(); //get taps/clicks for pause menu etc.
  252. if (mecanimEnabled)//reset parameters for next frame
  253. {
  254. aPlayerMecAnim.SetBool("StrafeLeftAnim", false);
  255. aPlayerMecAnim.SetBool("StrafeRightAnim", false);
  256. }
  257. if (bControlsEnabled)
  258. SwipeMovementControl();
  259. }//end of update()
  260. void FixedUpdate()
  261. {
  262. if (hInGameScriptCS.isGamePaused() == true)
  263. {
  264. //Debug.Log(hInGameScriptCS.isGamePaused() );
  265. return;
  266. }
  267. if (mecanimEnabled)//set position and rotation of the mesh to its original values
  268. {
  269. aPlayerMecAnim.transform.localPosition = v3DefaultPlayerAnimPosition;
  270. aPlayerMecAnim.transform.localEulerAngles = v3DefaultPlayerAnimRotation;
  271. }
  272. //Debug.Log("setForwardSpeed");
  273. setForwardSpeed();
  274. SetTransform();
  275. setShadow();
  276. if(!bInAir)
  277. {
  278. if(bExecuteLand)
  279. {
  280. hSoundManagerCS.playSound(SoundManagerCS.CharacterSounds.JumpLand);
  281. bExecuteLand = false;
  282. JumpAnimationFirstTime = true;
  283. }
  284. }//end of if not in air
  285. else
  286. {
  287. if(JumpAnimationFirstTime&&bInJump==true)
  288. {
  289. if (!mecanimEnabled)
  290. aPlayer.Rewind("jump");
  291. JumpAnimationFirstTime = false;
  292. bInDuck = false;
  293. PlayerController.Instance.Jump();
  294. if (!mecanimEnabled)
  295. aPlayer.CrossFade("jump", 0.1f);
  296. else
  297. {
  298. aPlayerMecAnim.SetBool("JumpAnim", true);
  299. }
  300. }
  301. }//end of else !in air
  302. if(bJumpFlag==true)
  303. {
  304. bJumpFlag = false;
  305. bExecuteLand = true;
  306. bInJump = true;
  307. bInAir = true;
  308. if (mecanimEnabled)//if mecanim animations are used
  309. {
  310. aPlayerMecAnim.SetBool("RunAnim", false);//disable run animation
  311. aPlayerMecAnim.SetBool("DuckAnim", false);//disable slide animation
  312. }
  313. fCurrentUpwardVelocity = fJumpPush;
  314. fCurrentHeight = tPlayer.position.y;
  315. hMissionsControllerCS.incrementMissionCount(MissionsControllerCS.MissionTypes.Jump);//count jumps for mission script
  316. hGlobalAchievementControllerCS.incrementAchievementCount(GlobalAchievementControllerCS.GlobalAchievementTypes.Jump);//count jumps for global achievements script
  317. }
  318. //acclerate movement speed with time
  319. if (fCurrentWalkSpeed < fEndingWalkSpeed && Accelerate)
  320. fCurrentWalkSpeed += (fCurrentWalkAccleration * Time.fixedDeltaTime);
  321. if (!mecanimEnabled)
  322. aPlayer["run"].speed = Mathf.Clamp( (fCurrentWalkSpeed/fStartingWalkSpeed)/1.1f, 0.8f, 1.2f ); //set run animation speed according to current speed
  323. }//end of Fixed Update
  324. /*
  325. * FUNCTION: Check if pause button is tapped in-game
  326. * CALLED BY: Update()
  327. */
  328. private void getClicks()
  329. {
  330. if(Input.GetMouseButtonUp(0) && bMouseReleased==true)
  331. {
  332. Vector3 screenPoint;
  333. Vector2 buttonSize;
  334. Rect Orb_Rect;
  335. if (tHUDGroup.localPosition.z==0)
  336. {
  337. buttonSize = new Vector2(Screen.width/6,Screen.width/6);
  338. screenPoint = HUDCamera.WorldToScreenPoint( tPauseButton.position );
  339. Orb_Rect = new Rect (screenPoint.x - ( buttonSize.x * 0.5f ), screenPoint.y - ( buttonSize.y * 0.5f ), buttonSize.x, buttonSize.y);
  340. if(Orb_Rect.Contains(Input.mousePosition))
  341. {
  342. hInGameScriptCS.pauseGame();
  343. }
  344. }
  345. //Orb_Rect = new Rect (screenPoint.x - ( buttonSize.x * 0.5f ), screenPoint.y - ( buttonSize.y * 0.5f ), buttonSize.x, buttonSize.y);
  346. }//end of mouserelease == true if
  347. }//end of get clicks function
  348. /*
  349. * FUNCITON: Set the position of the shadow under the player and of the
  350. * colliders to make them move with the character mesh.
  351. * CALLED BY: FixedUpdate()
  352. */
  353. private void setShadow()
  354. {
  355. tBlobShadowPlane.up = hitInfo.normal;
  356. //set shadow's position
  357. tBlobShadowPlane.position = new Vector3(tBlobShadowPlane.position.x, fContactPointY+2f, tBlobShadowPlane.position.z);
  358. //set shadow's rotation
  359. tBlobShadowPlane.localEulerAngles = new Vector3(tBlobShadowPlane.localEulerAngles.x,
  360. tPlayerRotation.localEulerAngles.y, tBlobShadowPlane.localEulerAngles.z);
  361. //set side collider's position and rotation
  362. tPlayerSidesCollider.position = tPlayer.position + new Vector3(0,5,0);
  363. tPlayerSidesCollider.localEulerAngles = tBlobShadowPlane.localEulerAngles;//set
  364. //set front collider's position and rotation
  365. tFrontCollider.position = tPlayer.position + new Vector3(7,5,0);
  366. tFrontCollider.localEulerAngles = tBlobShadowPlane.localEulerAngles;
  367. }
  368. /*
  369. * FUNCTION: Set the player's position the path with reference to the spline
  370. * CALLED BY: FixedUpdate()
  371. */
  372. private void SetTransform()
  373. {
  374. // Debug.Log("!!!! " + hInGameScriptCS.isGamePaused());
  375. int iStrafeDirection = (int)getLeftRightInput(); //get the current lane (-1, 0 or 1)
  376. fCurrentDistanceOnPath = hCheckPointsMainCS.SetNextMidPointandRotation(fCurrentDistanceOnPath, fCurrentForwardSpeed);//distance on current patch
  377. fCurrentDistance = fCurrentDistanceOnPath + hPatchesRandomizerCS.getCoveredDistance();//total distance since the begining of the run
  378. fCurrentMileage = fCurrentDistance/12.0f;//calculate milage to display score on HUD
  379. tCurrentAngle = hCheckPointsMainCS.getCurrentAngle();//get the angle according to the position on path
  380. //set player rotation according to the current player position on the path's curve (if any)
  381. tPlayerRotation.localEulerAngles = new Vector3(tPlayerRotation.localEulerAngles.x, -tCurrentAngle, tPlayerRotation.localEulerAngles.z);
  382. CurrentDirection = hCheckPointsMainCS.getCurrentDirection();
  383. Vector3 Desired_Horinzontal_Pos = calculateHorizontalPosition(iStrafeDirection);
  384. //Debug.Log(Desired_Horinzontal_Pos);
  385. bGroundhit = Physics.Linecast(Desired_Horinzontal_Pos + new Vector3(0,20,0),Desired_Horinzontal_Pos + new Vector3(0,-100,0), out hitInfo,(1<<9));
  386. if(bGroundhit && hPitsMainControllerCS.isFallingInPit()==false)//calculate player position in y-axis
  387. fContactPointY = hitInfo.point.y;
  388. else//call death if player in not on Terrain_lyr
  389. {
  390. Debug.Log("call death if player in not on Terrain_lyr");
  391. fContactPointY = -10000.0f;
  392. if(!bInAir)
  393. {
  394. if(!bInJump)
  395. {
  396. if(reConfirmPitFalling(Desired_Horinzontal_Pos,iStrafeDirection)==true)
  397. {
  398. hPitsMainControllerCS.setPitValues();
  399. }
  400. }
  401. bInAir = true;
  402. fCurrentUpwardVelocity = 0;
  403. fCurrentHeight = tPlayer.position.y;
  404. }
  405. }
  406. if(!bInAir)//set player position when not in air
  407. {
  408. //Debug.Log("//set player position when not in air");
  409. tPlayer.position = new Vector3(tPlayer.position.x,
  410. fContactPointY+0.6f, tPlayer.position.z);
  411. }
  412. else//set player position if in air
  413. {
  414. //Debug.Log("/set player position if in air");
  415. if (bDiveFlag) //dive during jump
  416. {
  417. setCurrentDiveHeight();
  418. tPlayer.position = new Vector3(tPlayer.position.x,
  419. fCurrentHeight, tPlayer.position.z);
  420. }
  421. else //JUMP
  422. {
  423. setCurrentJumpHeight();
  424. tPlayer.position = new Vector3(tPlayer.position.x,
  425. fCurrentHeight, tPlayer.position.z);
  426. }
  427. }
  428. tPlayer.position = new Vector3(Desired_Horinzontal_Pos.x,
  429. tPlayer.position.y, Desired_Horinzontal_Pos.z);//set player position in x and z axis
  430. }//end of Set Transform()
  431. /*
  432. * FUNCTION: Set the height of the player during jump
  433. * CALLED BY: SetTransform()
  434. */
  435. private void setCurrentJumpHeight() //set height during jump
  436. {
  437. fCurrentUpwardVelocity-=Time.fixedDeltaTime*getAccleration();
  438. fCurrentUpwardVelocity = Mathf.Clamp(fCurrentUpwardVelocity,-fJumpPush,fJumpPush);
  439. fCurrentHeight+=fCurrentUpwardVelocity*(Time.fixedDeltaTime/1.4f);
  440. if(fCurrentHeight<fContactPointY)
  441. {
  442. fCurrentHeight = fContactPointY;
  443. bInAir = false;
  444. bInJump = false;
  445. if (bDiveFlag) //do not resume run animation on Dive
  446. return;
  447. if (!hInGameScriptCS.isEnergyZero())
  448. {
  449. if (!mecanimEnabled)
  450. {
  451. aPlayer.CrossFade("run", 0.1f);
  452. PlayerController.Instance.Run();
  453. }
  454. else
  455. {
  456. aPlayerMecAnim.SetBool("JumpAnim", false);
  457. aPlayerMecAnim.SetBool("RunAnim", true);
  458. }
  459. }//end of if current energy > 0
  460. }
  461. }
  462. /*
  463. * FUNCITON: Pull the player down faster if user swipes down int the middle of jump
  464. * CALLED BY: SetTransform()
  465. */
  466. private void setCurrentDiveHeight() //set height after dive called
  467. {
  468. fCurrentUpwardVelocity-=Time.fixedDeltaTime*2000;
  469. fCurrentUpwardVelocity = Mathf.Clamp(fCurrentUpwardVelocity,-fJumpPush,fJumpPush);
  470. if(hPitsMainControllerCS.isFallingInPit() == false)
  471. fCurrentHeight+=fCurrentUpwardVelocity*Time.fixedDeltaTime;
  472. else
  473. {
  474. fCurrentHeight-=40.0f*Time.fixedDeltaTime;
  475. hMenuScriptCS.hideHUDElements();
  476. }
  477. if(fCurrentHeight<=fContactPointY)
  478. {
  479. fCurrentHeight = fContactPointY;//bring character down completely
  480. bInAir = false;
  481. bInJump = false;
  482. if (mecanimEnabled)
  483. aPlayerMecAnim.SetBool("JumpAnim", false);
  484. duckPlayer();//make the character slide
  485. bDiveFlag = false; //dive complete
  486. }//end of if
  487. }
  488. /*
  489. * FUNCTION: Make sure that there is no terrain under the player
  490. * before making it fall
  491. * CALLED BY: SetTransform()
  492. */
  493. private bool reConfirmPitFalling(Vector3 Desired_Horinzontal_Pos, float iStrafeDirection)
  494. {
  495. bool bGroundhit = false;
  496. if(iStrafeDirection>=0)
  497. bGroundhit = Physics.Linecast(Desired_Horinzontal_Pos + new Vector3(1,20,5),Desired_Horinzontal_Pos + new Vector3(0,-100,5), out hitInfo,1<<9);
  498. else
  499. bGroundhit = Physics.Linecast(Desired_Horinzontal_Pos + new Vector3(1,20,-5),Desired_Horinzontal_Pos + new Vector3(0,-100,-5), out hitInfo,1<<9);
  500. if(!bGroundhit)
  501. return true;
  502. else
  503. return false;
  504. }
  505. /*
  506. * FUNCTION: Called when user runs out of energy
  507. * CALLED BY: Update()
  508. */
  509. private bool DeathScene()
  510. {
  511. if (PowerupsMainControllerCS.Instance.SuperSledgeActive)
  512. {
  513. return false;
  514. }
  515. bInAir = false;
  516. tPlayerRotation.localEulerAngles = new Vector3(0,0,0);
  517. //Debug.Log(" DeathScene()");
  518. if (fDeathAnimStartTime == 0)
  519. {
  520. //Debug.Log(" DeathScene()1");
  521. hSoundManagerCS.stopSound(SoundManagerCS.CharacterSounds.Footsteps);
  522. bControlsEnabled = false;
  523. if (!mecanimEnabled) //if legacy animations enabled
  524. {
  525. aPlayer.CrossFade("death",0.1f);
  526. PlayerController.Instance.Death();
  527. //hInGameScriptCS.setupDeathMenu();
  528. }
  529. else//if mecanim enabled
  530. {
  531. aPlayerMecAnim.SetBool("DeathAnim", true);
  532. aPlayerMecAnim.SetBool("RunAnim", false);
  533. }
  534. hEnemyControllerCS.playDeathAnimation();
  535. if (hInGameScriptCS.isCustomMenuEnabled())
  536. hMenuScriptCS.hideHUDElements();
  537. fDeathAnimStartTime = Time.time;
  538. }
  539. else if (fDeathAnimStartTime != 0 && (Time.time - fDeathAnimStartTime) >= iDeathAnimEndTime && RealDeath)
  540. {
  541. //Debug.Log(" DeathScene()2");
  542. //hInGameScriptCS.setupDeathMenu();
  543. InGameScriptCS.Instance.PendingResurection();
  544. RealDeath = false;
  545. return true;
  546. }
  547. // Debug.Log(" DeathScene()3");
  548. return false;
  549. }
  550. public bool RealDeath = true;
  551. public void Active()
  552. {
  553. bInAir = true;
  554. hPlayerFrontColliderScriptCS.activateCollider();
  555. hPlayerSidesColliderScriptCS.activateCollider();
  556. hSoundManagerCS.playSound(SoundManagerCS.CharacterSounds.Footsteps);
  557. bControlsEnabled = true;
  558. if (!mecanimEnabled) //if legacy animations enabled
  559. {
  560. aPlayer.CrossFade("death", 0.1f);
  561. PlayerController.Instance.Alive();
  562. //hInGameScriptCS.setupDeathMenu();
  563. }
  564. else//if mecanim enabled
  565. {
  566. aPlayerMecAnim.SetBool("DeathAnim", true);
  567. aPlayerMecAnim.SetBool("RunAnim", false);
  568. PlayerController.Instance.Alive();
  569. }
  570. //hEnemyControllerCS.playDeathAnimation();
  571. if (hInGameScriptCS.isCustomMenuEnabled())
  572. hMenuScriptCS.hideHUDElements();
  573. //fDeathAnimStartTime = Time.time;
  574. }
  575. /*
  576. * Broadcast target
  577. * Fixes "no slide" bug
  578. */
  579. public void RestoreNoSlide()
  580. {
  581. bInDuck = false;
  582. tFrontCollider.localScale = v3BNCDefaultScale;
  583. tPlayerSidesCollider.localScale = v3BFCDefaultScale;//restore far collider
  584. }
  585. /*
  586. * FUNCTION: Called when player hits an obstacle sideways
  587. * CALLED BY: PlayerSidesColliderScript.OnCollisionEnter()
  588. */
  589. public void processStumble()
  590. {
  591. //hCameraControllerCS.setCameraShakeImpulseValue(1);
  592. iLanePosition = iLastLanePosition; //stop strafe
  593. if (hEnemyControllerCS.processStumble())
  594. {
  595. hInGameScriptCS.collidedWithObstacle();//call death if player stumbled twice in unit time
  596. }
  597. else
  598. {
  599. if (!mecanimEnabled)
  600. aPlayer.PlayQueued("run", QueueMode.CompleteOthers);
  601. //enable colliders if they were disabled
  602. hPlayerFrontColliderScriptCS.activateCollider();
  603. hPlayerSidesColliderScriptCS.activateCollider();
  604. }
  605. }
  606. /*
  607. * FUNCTION: Returns horizontal the position to move to
  608. * CALLED BY: SetTransform()
  609. */
  610. private float getLeftRightInput() //change lane
  611. {
  612. if (swipeControlsEnabled == true)//swipe direction
  613. return iLanePosition;
  614. else//gyro direction
  615. {
  616. float fMovement = 0.0f;
  617. float fSign = 1.0f;
  618. if(Screen.orientation == ScreenOrientation.Portrait)
  619. fSign = 1.0f;
  620. else
  621. fSign = -1.0f;
  622. if(Application.isEditor)//map gyro controls on mouse in editor mode
  623. {
  624. fMovement = (Input.mousePosition.x - (Screen.height/2.0f))/(Screen.height/2.0f) * 4.5f;
  625. }
  626. else
  627. {
  628. fMovement = (fSign * Input.acceleration.x * 4.5f);
  629. }
  630. return fMovement;
  631. }
  632. }
  633. /*
  634. * FUNCTION: Set the movement speed
  635. * CALLED BY: FixedUpdate()
  636. */
  637. private void setForwardSpeed()
  638. {
  639. //if the player is not on Terrain_lyr
  640. //if(hPitsMainControllerCS.isFallingInPit())
  641. //{
  642. // if (transform.position.x > fPitPositionX)
  643. // {
  644. // Debug.LogWarning("fPitPositionX set 0");
  645. // fCurrentForwardSpeed = 0;
  646. // }
  647. // else
  648. // {
  649. // Debug.LogWarning("fPitPositionX set speed");
  650. // fCurrentForwardSpeed = Mathf.Lerp(fPitFallForwardSpeed, 0.01f, (Time.time - fPitFallLerpValue)*3.5f);
  651. // }
  652. // return;
  653. //}
  654. if (hInGameScriptCS.isEnergyZero())//on death
  655. {
  656. fCurrentForwardSpeed = 0;
  657. return;
  658. }
  659. fForwardAccleration = bInAir ? 1 : 2;
  660. fJumpForwardFactor = 1 + ((1/fCurrentWalkSpeed)*50);
  661. if (bInJump)
  662. {
  663. //Debug.LogWarning("InJump set speed");
  664. fCurrentForwardSpeed = Mathf.Lerp(fCurrentForwardSpeed,
  665. fCurrentWalkSpeed*Time.fixedDeltaTime*fJumpForwardFactor, Time.fixedDeltaTime*fForwardAccleration);
  666. }
  667. else
  668. {
  669. //Debug.LogWarning("Groundset speed");
  670. fCurrentForwardSpeed = Mathf.Lerp(fCurrentForwardSpeed, (fCurrentWalkSpeed)*Time.fixedDeltaTime,
  671. Time.fixedDeltaTime*fForwardAccleration);
  672. }
  673. }
  674. /*
  675. * FUNCTION: Make the player change lanes
  676. * CALLED BY: SetTransform()
  677. */
  678. private float fCurrentStrafePosition = 0.0f; //keeps track of strafe position at each frame
  679. private float fSpeedMultiplier = 5.0f; //how fast to strafe/ change lane
  680. private Vector3 calculateHorizontalPosition(int iStrafeDirection)
  681. {
  682. Vector2 fHorizontalPoint;
  683. Vector2 SideDirection_Vector2;
  684. if (swipeControlsEnabled)
  685. {
  686. SideDirection_Vector2 = rotateAlongZAxis(new Vector2(CurrentDirection.x,CurrentDirection.z),90.0f);
  687. SideDirection_Vector2.Normalize();
  688. if(iStrafeDirection==-1)//strafe left from center
  689. {
  690. if(fCurrentStrafePosition>-1)
  691. {
  692. fCurrentStrafePosition-= fSpeedMultiplier*Time.fixedDeltaTime;
  693. if(fCurrentStrafePosition<=-1.0f)
  694. {
  695. fCurrentStrafePosition = -1.0f;
  696. switchStrafeToSprint();
  697. }
  698. }
  699. }
  700. else if(iStrafeDirection==1)//strafe right from center
  701. {
  702. if(fCurrentStrafePosition<1)
  703. {
  704. fCurrentStrafePosition+= fSpeedMultiplier*Time.fixedDeltaTime;
  705. if(fCurrentStrafePosition>=1.0f)
  706. {
  707. fCurrentStrafePosition = 1.0f;
  708. switchStrafeToSprint();
  709. }
  710. }
  711. }
  712. else if(iStrafeDirection==0&&fCurrentStrafePosition!=0.0f)//strafe from left or right lane to center
  713. {
  714. if(fCurrentStrafePosition<0)
  715. {
  716. fCurrentStrafePosition+= fSpeedMultiplier*Time.fixedDeltaTime;
  717. if(fCurrentStrafePosition>=0.0f)
  718. {
  719. fCurrentStrafePosition = 0.0f;
  720. switchStrafeToSprint();
  721. }
  722. }
  723. else if(fCurrentStrafePosition>0)
  724. {
  725. fCurrentStrafePosition-= fSpeedMultiplier*Time.fixedDeltaTime;
  726. if(fCurrentStrafePosition<=0.0f)
  727. {
  728. fCurrentStrafePosition = 0.0f;
  729. switchStrafeToSprint();
  730. }
  731. }
  732. }//end of else
  733. fHorizontalDistance = -fCurrentStrafePosition*16.0f;
  734. fHorizontalDistance = Mathf.Clamp(fHorizontalDistance,-20.0f,20.0f);
  735. fHorizontalPoint = hCheckPointsMainCS.getCurrentMidPoint() + SideDirection_Vector2*fHorizontalDistance;
  736. return new Vector3(fHorizontalPoint.x,tPlayerRotation.position.y,fHorizontalPoint.y);
  737. }
  738. else
  739. {
  740. SideDirection_Vector2 = rotateAlongZAxis(new Vector2(CurrentDirection.x,CurrentDirection.z),90.0f);
  741. SideDirection_Vector2.Normalize();
  742. fHorizontalDistance = Mathf.Lerp(fHorizontalDistance,-iStrafeDirection * 40.0f, 0.05f*fCurrentForwardSpeed);
  743. fHorizontalDistance = Mathf.Clamp(fHorizontalDistance,-20.0f,20.0f);
  744. fHorizontalPoint = hCheckPointsMainCS.getCurrentMidPoint() + SideDirection_Vector2*fHorizontalDistance;
  745. return new Vector3(fHorizontalPoint.x,tPlayerRotation.position.y,fHorizontalPoint.y);
  746. }//end of else
  747. }
  748. /*
  749. * FUNCTION: Determine the rotation of the player character
  750. */
  751. private Vector2 rotateAlongZAxis(Vector2 inputVector, float angletoRotate)
  752. {
  753. Vector2 FinalVector = Vector2.zero;
  754. angletoRotate = angletoRotate/57.3f;
  755. FinalVector.x = Mathf.Cos(angletoRotate) * inputVector.x - Mathf.Sin(angletoRotate) * inputVector.y;
  756. FinalVector.y = Mathf.Sin(angletoRotate) * inputVector.x + Mathf.Cos(angletoRotate) * inputVector.y;
  757. return FinalVector;
  758. }
  759. /*
  760. * FUNCTION: Play the "run" animation
  761. * CALLED BY: calculateHorizontalPosition()
  762. */
  763. private void switchStrafeToSprint()
  764. {
  765. if (!hInGameScriptCS.isEnergyZero() && !isInAir())
  766. {
  767. if (!mecanimEnabled)
  768. {
  769. aPlayer.CrossFade("run", 0.1f);
  770. PlayerController.Instance.Run();
  771. }
  772. else
  773. aPlayerMecAnim.SetBool("RunAnim",true);
  774. bInStrafe = false;
  775. }
  776. }
  777. /*
  778. * FUNCITON: Detect swipes on screen
  779. * CALLED BY: Update()
  780. */
  781. void SwipeMovementControl()
  782. {
  783. //check and execute two jump or duck commands simultaneously
  784. if (bDirectionQueueFlag)
  785. {
  786. if(!bInAir && directionQueue == SwipeControlsCS.SwipeDirection.Jump) //queue JUMP
  787. {
  788. bJumpFlag = true;
  789. bDirectionQueueFlag = false;
  790. }//end of jump queue
  791. if (directionQueue == SwipeControlsCS.SwipeDirection.Duck && !bInDuck) //queue SLIDE
  792. {
  793. duckPlayer();
  794. bDirectionQueueFlag = false;
  795. }//end of duck queue
  796. }//end of direction queue
  797. //restore the size of the collider after slide ends
  798. if ( (mecanimEnabled && aPlayerMecAnim.GetAnimatorTransitionInfo(0).nameHash == Animator.StringToHash("Base.Slide -> Base.Run"))
  799. || (!mecanimEnabled && !isPlayingDuck() && bInDuck == true) )//is the slide animation playing?
  800. {
  801. hSoundManagerCS.playSound(SoundManagerCS.CharacterSounds.Footsteps);
  802. //rotation correction after DIVE
  803. tPlayerRotation.localEulerAngles = new Vector3(tPlayerRotation.localEulerAngles.x, tPlayerRotation.localEulerAngles.y,0);
  804. //translation correction after DIVE (to fix mysterious bug :S)
  805. tBlobShadowPlane.localPosition = new Vector3(0, tBlobShadowPlane.localPosition.y, tBlobShadowPlane.localPosition.z);
  806. bInDuck = false;
  807. tFrontCollider.localScale = v3BNCDefaultScale;
  808. tPlayerSidesCollider.localScale = v3BFCDefaultScale;//restore far collider
  809. if (bDiveFlag) //do not resume run animation on Dive
  810. return;
  811. if (!mecanimEnabled)
  812. aPlayer.CrossFadeQueued("run", 0.5f, QueueMode.CompleteOthers);
  813. else
  814. {
  815. aPlayerMecAnim.SetBool("DuckAnim", false);
  816. aPlayerMecAnim.SetBool("RunAnim", true);
  817. }
  818. }//end of if end of duck animation
  819. //swipe controls
  820. var direction = swipeLogic.getSwipeDirection(); //get the swipe direction
  821. if (direction != SwipeControlsCS.SwipeDirection.Null)
  822. {
  823. bMouseReleased = false;//disallow taps on swipe
  824. if (direction == SwipeControlsCS.SwipeDirection.Jump) //JUMP
  825. {
  826. if(!bInAir)
  827. {
  828. bJumpFlag = true;
  829. }
  830. if (bInAir) //queue the second jump if player swipes up in the middle of a jump
  831. {
  832. bDirectionQueueFlag = true;
  833. directionQueue = SwipeControlsCS.SwipeDirection.Jump;
  834. }
  835. }//end of if direction is jump
  836. if (direction == SwipeControlsCS.SwipeDirection.Right && swipeControlsEnabled == true) //RIGHT swipe
  837. {
  838. if (iLanePosition != 1)
  839. {
  840. iLastLanePosition = iLanePosition;
  841. iLanePosition++;
  842. strafePlayer(StrafeDirection.Strafe_Right);
  843. }//end of lane check if
  844. }//end of swipe direction if
  845. if (direction == SwipeControlsCS.SwipeDirection.Left && swipeControlsEnabled == true) //LEFT swipe
  846. {
  847. if (iLanePosition != -1)
  848. {
  849. iLastLanePosition = iLanePosition;
  850. iLanePosition--;
  851. strafePlayer(StrafeDirection.Strafe_Left);
  852. }//end of lane check if
  853. }//end of swipe direction if
  854. if (direction == SwipeControlsCS.SwipeDirection.Duck && bInDuck)//SLIDE: queue the second duck command if player is in the middle of slide animation
  855. {
  856. bDirectionQueueFlag = true;
  857. directionQueue = SwipeControlsCS.SwipeDirection.Duck;
  858. }
  859. if (direction == SwipeControlsCS.SwipeDirection.Duck && !bInAir && !bInDuck)//SLIDE: on ground
  860. {
  861. duckPlayer();
  862. }
  863. if (direction == SwipeControlsCS.SwipeDirection.Duck && bInAir && !bInDuck)//SLIDE/ DIVE: in air
  864. {
  865. bDiveFlag = true; //used by Set Transform() to make the character dive
  866. }//end of slide in air if
  867. //swipeLogic.iTouchStateFlag = 2;
  868. }//end of if
  869. if (Input.GetMouseButtonUp(0)) //allow taps on mouse/ tap release
  870. {
  871. bMouseReleased = true;
  872. }
  873. //keyboard controls (DEBUG)
  874. if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))//Up/ jump
  875. {
  876. if(!bInAir)
  877. {
  878. bJumpFlag = true;
  879. }
  880. if (bInAir)
  881. {
  882. bDirectionQueueFlag = true;
  883. directionQueue = SwipeControlsCS.SwipeDirection.Jump;
  884. }
  885. }
  886. else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))//Right
  887. {
  888. if (iLanePosition != 1)
  889. {
  890. iLastLanePosition = iLanePosition;
  891. iLanePosition++;
  892. strafePlayer(StrafeDirection.Strafe_Right);
  893. }//end of lane check if
  894. }
  895. else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))//Left
  896. {
  897. if (iLanePosition != -1)
  898. {
  899. iLastLanePosition = iLanePosition;
  900. iLanePosition--;
  901. strafePlayer(StrafeDirection.Strafe_Left);
  902. }//end of lane check if
  903. }
  904. else if ( (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && bInDuck)
  905. {
  906. bDirectionQueueFlag = true;
  907. directionQueue = SwipeControlsCS.SwipeDirection.Duck;
  908. }
  909. else if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && !bInAir && !bInDuck)
  910. {
  911. duckPlayer();
  912. }
  913. else if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && bInAir && !bInDuck)
  914. {
  915. bDiveFlag = true; //used by Set Transform() to make the character dive
  916. }
  917. }//end of Movement Control function
  918. /*
  919. * FUNCTION: make the character slide
  920. * CALLED BY: SwipeMovementControl()
  921. */
  922. private void duckPlayer()
  923. {
  924. bInDuck = true;
  925. hSoundManagerCS.stopSound(SoundManagerCS.CharacterSounds.Footsteps);
  926. if (!mecanimEnabled)//if legacy animations are used
  927. {
  928. aPlayer["slide"].speed = 1.4f;
  929. aPlayer.CrossFade("slide", 0.1f);
  930. PlayerController.Instance.Slide();
  931. }
  932. else//if mecanim are used
  933. {
  934. aPlayerMecAnim.SetBool("DuckAnim", true);
  935. }
  936. tFrontCollider.localScale = v3BNCDefaultScale/2; //reduce the near collider size
  937. tPlayerSidesCollider.localScale = v3BFCDefaultScale/2; //reduce the far collider size
  938. hMissionsControllerCS.incrementMissionCount(MissionsControllerCS.MissionTypes.Duck);//count ducks for mission script
  939. hGlobalAchievementControllerCS.incrementAchievementCount(GlobalAchievementControllerCS.GlobalAchievementTypes.Duck);//count ducks for global achievements script
  940. }
  941. /*
  942. * FUNCTION: Check if the user is sliding
  943. */
  944. private bool isPlayingDuck()
  945. {
  946. if (hInGameScriptCS.isEnergyZero())
  947. return false;
  948. if (!mecanimEnabled)
  949. {
  950. if (aPlayer.IsPlaying("slide"))
  951. return true;
  952. else
  953. return false;
  954. }
  955. else
  956. return aPlayerMecAnim.GetBool("DuckAnim");
  957. }
  958. /*
  959. * FUNCTION: strafe charater right or left
  960. * INPUT: "right" OR "left"
  961. * OUTPUT: move the character left or right
  962. */
  963. private void strafePlayer(StrafeDirection strafeDirection)
  964. {
  965. if (!mecanimEnabled)
  966. {
  967. string anim = strafeDirection == StrafeDirection.Strafe_Right ? "right" : "left";
  968. if (isInAir())
  969. {
  970. aPlayer[anim].speed = 2;
  971. aPlayer.Play(anim);
  972. //aPlayer.CrossFade("glide", 0.5);
  973. }
  974. else if (aPlayer.IsPlaying(anim)) //if strafed while already strafing
  975. {
  976. aPlayer.Stop(anim);
  977. aPlayer[anim].speed = 1.75f;
  978. aPlayer.CrossFade(anim,0.01f);
  979. bInStrafe = true;
  980. }
  981. else
  982. {
  983. aPlayer[anim].speed = 1.75f;
  984. aPlayer.CrossFade(anim,0.01f);
  985. bInStrafe = true;
  986. }
  987. if (bInStrafe)
  988. {
  989. if (strafeDirection == StrafeDirection.Strafe_Right)
  990. {
  991. PlayerController.Instance.StrafeRight();
  992. }
  993. else
  994. {
  995. PlayerController.Instance.StrafeLeft();
  996. }
  997. }
  998. }//end of if is MechAnim Enabled
  999. else
  1000. {
  1001. string anim = strafeDirection == StrafeDirection.Strafe_Right ? "StrafeRightAnim" : "StrafeLeftAnim";
  1002. if (isInAir())
  1003. {
  1004. aPlayerMecAnim.SetBool(anim, true);
  1005. }
  1006. else if (aPlayerMecAnim.GetBool("StrafeRightAnim") || aPlayerMecAnim.GetBool("StrafeLeftAnim")) //if strafed while already strafing
  1007. {
  1008. aPlayerMecAnim.SetBool(anim, true);
  1009. bInStrafe = true;
  1010. }
  1011. else
  1012. {
  1013. aPlayerMecAnim.SetBool(anim, true);
  1014. bInStrafe = true;
  1015. }
  1016. }
  1017. }//end of strafe player function
  1018. public float getCurrentMileage() { return fCurrentMileage; }
  1019. public float getCurrentDistance() { return fCurrentDistance; }
  1020. public float getCurrentForwardSpeed() { return fCurrentForwardSpeed; }
  1021. public int getCurrentLane() { return iLanePosition; }
  1022. public float getCurrentPlayerRotation() { return tCurrentAngle; }
  1023. public float getCurrentWalkSpeed() { return fCurrentWalkSpeed; }
  1024. public bool isInAir()
  1025. {
  1026. if (bInAir || bJumpFlag || bInJump || bDiveFlag)
  1027. return true;
  1028. else
  1029. return false;
  1030. }
  1031. public void setCurrentDistanceOnPath(float iValue) { fCurrentDistanceOnPath = iValue; }
  1032. public void setPitFallLerpValue(float iValue) { fPitFallLerpValue = iValue; }
  1033. public void setPitFallForwardSpeed(float iValue) { fPitFallForwardSpeed = iValue; }
  1034. /*
  1035. * FUNCTION: Turn player animations On or Off
  1036. */
  1037. public void togglePlayerAnimation(bool bValue) { aPlayer.enabled = bValue; }
  1038. public bool isMechAnimEnabled() { return mecanimEnabled; }
  1039. }