Advertisement
Guest User

Untitled

a guest
Aug 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityStandardAssets.CrossPlatformInput;
  6.  
  7. public class PlayerController : MonoBehaviour {
  8.  
  9. [Header("General")]
  10. [Tooltip("In ms^-1")][SerializeField] float controlSpeed = 10f;
  11. [Tooltip("In m1")] [SerializeField] float xRange = 5f;
  12. [Tooltip("In m1")] [SerializeField] float yRange = 5f;
  13. [SerializeField] GameObject[] Guns;
  14.  
  15. [Header("Screen-position Based")]
  16. [SerializeField] float positionPitchFactor = -2f;
  17. [SerializeField] float positionYawFactor = 4f;
  18.  
  19. [Header("Control-throw Based")]
  20. [SerializeField] float controlPitchFactor = -15f;
  21. [SerializeField] float controlRollFactor = -20f;
  22.  
  23. float xThrow, yThrow;
  24. bool isControlEnabled = true;
  25. // Use this for initialization
  26.  
  27. // Update is called once per frame
  28. void Update ()
  29. {
  30. if (isControlEnabled)
  31. {
  32. ProcessTranslation();
  33. ProcessRotation();
  34. ProcessFiring();
  35. }
  36.  
  37. }
  38. void die()
  39. {
  40. isControlEnabled = false;
  41. }
  42. void OnPlayerDeath() // called by string reference
  43. {
  44. Invoke("die", 10);
  45. }
  46. private void ProcessRotation()
  47. {
  48. float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
  49. float pitchDueToControlThrow = yThrow * controlPitchFactor;
  50. float pitch = pitchDueToPosition + pitchDueToControlThrow;
  51.  
  52. float yaw = transform.localPosition.x * positionYawFactor;
  53.  
  54. float roll = xThrow * controlRollFactor;
  55.  
  56. transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
  57. }
  58.  
  59. private void ProcessTranslation()
  60. {
  61. xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
  62. yThrow = CrossPlatformInputManager.GetAxis("Vertical");
  63.  
  64. float xOffset = xThrow * controlSpeed * Time.deltaTime;
  65. float yOffset = yThrow * controlSpeed * Time.deltaTime;
  66.  
  67. float rawXPos = transform.localPosition.x + xOffset;
  68. float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
  69.  
  70. float rawYPos = transform.localPosition.y + yOffset;
  71. float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);
  72.  
  73. transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
  74. }
  75. void ProcessFiring()
  76. {
  77. if (CrossPlatformInputManager.GetButton("fire"))
  78. {
  79. SetGunsActive(true);
  80. }
  81. else
  82. {
  83. SetGunsActive(false);
  84. }
  85. }
  86.  
  87. private void SetGunsActive(bool isActive)
  88. {
  89. foreach(GameObject gun in Guns)
  90. {
  91. var emmisionModual = gun.GetComponent<ParticleSystem>().emission;
  92. emmisionModual.enabled = isActive;
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement