You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
8.0 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class magnetGun : MonoBehaviour {
  5. public Camera camera;
  6. public GameObject crossHair;
  7. public thirdPersonController playerController;
  8. public magnetGun otherPlayer;
  9. public string L_TRIGGER_INPUT;
  10. public string R_TRIGGER_INPUT;
  11. public string L_BUTTON_INPUT;
  12. public string R_BUTTON_INPUT;
  13. private bool L_Trigger_Down;
  14. public bool topScreen = true;
  15. private float playerScreen = 2;
  16. public GameObject gravityWell;
  17. public Collider GravityTarget;
  18. private Vector3 rayHitPoint;
  19. private Vector3 targetOffset;
  20. public float objectSpeed;
  21. public float magnetRange = 20;
  22. public float maxPullSpeed = 30;
  23. public float rotateSpeed;
  24. public float minDistance = 3.0f;
  25. public float closeRange = 5.0f;
  26. public float impulsePower = 50.0f;
  27. private RigidbodyConstraints originalConstrants;
  28. private bool originalGravity;
  29. private bool axisDown = false;
  30. private float normGrip;
  31. private Renderer lastTargetRenderer;
  32. private Color lastTargetColour;
  33. // Use this for initialization
  34. void Start () {
  35. if (topScreen)
  36. playerScreen = 2;
  37. else
  38. playerScreen = -1;
  39. normGrip = playerController.grip;
  40. }
  41. // Update is called once per frame
  42. void Update () {
  43. if (playerController.active) {
  44. float triggerL = Input.GetAxis (L_TRIGGER_INPUT);
  45. float triggerR = Input.GetAxis (R_TRIGGER_INPUT);
  46. Collider target = testItem ();
  47. crossHair.GetComponent<RawImage> ().color = Color.white;
  48. if (target != null) {
  49. if (target.GetComponent<Collider> ().tag == "moveable") {
  50. Debug.Log ("Moveable Item");
  51. crossHair.GetComponent<RawImage> ().color = Color.red;
  52. if (Vector3.Distance (rayHitPoint, camera.transform.position) <= closeRange) {
  53. crossHair.GetComponent<RawImage> ().color = Color.blue;
  54. }
  55. }
  56. }
  57. updateColors (target);
  58. if (getAxisDown (L_TRIGGER_INPUT)) {
  59. Debug.Log ("axis down");
  60. //Debug.Log (GravityTarget.name);
  61. if (GravityTarget != null) {
  62. dropItem (GravityTarget);
  63. Debug.Log ("dropping item");
  64. } else {
  65. pickUpItem (target);
  66. }
  67. }
  68. /* (triggerL > 0) {
  69. if (!L_Trigger_Down) {
  70. pickUpItem (target);
  71. L_Trigger_Down = true;
  72. }
  73. } else {
  74. L_Trigger_Down = false;
  75. dropItem(GravityTarget);
  76. }
  77. */
  78. if ( GravityTarget == null)
  79. impulsePush (target);
  80. else
  81. pullItem (triggerR);
  82. }
  83. moveItem (GravityTarget);
  84. rotateInput (GravityTarget);
  85. cameraRotateTest (GravityTarget);
  86. }
  87. private Collider testItem(){
  88. RaycastHit hit;
  89. //Vector3 rayDirection = camera.transform.rotation * Vector3.forward;
  90. //Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2 + (Screen.height/6 * playerScreen), 0));
  91. Ray ray = camera.ScreenPointToRay(crossHair.transform.position);
  92. Debug.DrawRay (ray.origin, ray.direction*magnetRange, Color.green);
  93. if (Physics.Raycast (ray, out hit, magnetRange)) {
  94. if (hit.collider.tag == "moveable"){
  95. Debug.DrawRay (ray.origin,ray.direction*magnetRange, Color.red);
  96. rayHitPoint = hit.point;
  97. }
  98. }
  99. return hit.collider;
  100. }
  101. private void pickUpItem(Collider item){
  102. if (item != null) {
  103. if (item == otherPlayer.GravityTarget){
  104. otherPlayer.dropItem(item);
  105. }
  106. if (item.tag == "moveable"){
  107. originalConstrants = item.attachedRigidbody.constraints;
  108. originalGravity = item.attachedRigidbody.useGravity;
  109. item.attachedRigidbody.useGravity = false;
  110. item.attachedRigidbody.drag = 3.0f;
  111. item.attachedRigidbody.constraints = RigidbodyConstraints.FreezeRotation | originalConstrants;
  112. //camera.transform.LookAt(item.transform.position);
  113. //playerController.cameraX = camera.transform.eulerAngles.x;
  114. //playerController.cameraY = camera.transform.eulerAngles.y;
  115. gravityWell.transform.position = rayHitPoint;
  116. GravityTarget = item;
  117. targetOffset = GravityTarget.transform.position - rayHitPoint + (Vector3.up * 0.05f);
  118. playerController.slowed = true;
  119. playerController.movementLock = true;
  120. playerController.grip = 15.0f;
  121. if (!topScreen){
  122. playerController.cameraSpeedX = 0.0f;
  123. playerController.cameraSpeedY = 0.0f;
  124. }
  125. }
  126. }
  127. }
  128. private void impulsePush (Collider item){
  129. if (item != null) {
  130. if (item.tag == "moveable" && Input.GetButtonDown("Quick Push")){
  131. if (item == otherPlayer.GravityTarget){
  132. otherPlayer.dropItem(item);
  133. }
  134. Vector3 direction = (transform.position - item.transform.position).normalized;
  135. item.attachedRigidbody.AddForce(direction * impulsePower * maxPullSpeed* item.attachedRigidbody.mass,ForceMode.Impulse );
  136. }
  137. }
  138. }
  139. private void moveItem(Collider item){
  140. if (item != null) {
  141. if (item != playerController.curCollider){
  142. float step = objectSpeed * Time.deltaTime;
  143. Vector3 direction = gravityWell.transform.position - item.transform.position + targetOffset;
  144. direction = Vector3.ClampMagnitude(direction,1.0f);
  145. item.attachedRigidbody.AddForce(direction * objectSpeed * Time.deltaTime);
  146. }
  147. }
  148. }
  149. private void dropItem(Collider item){
  150. if (item != null) {
  151. item.attachedRigidbody.useGravity = originalGravity;
  152. item.attachedRigidbody.drag = 0.0f;
  153. item.attachedRigidbody.constraints = originalConstrants;
  154. if(topScreen)
  155. item.attachedRigidbody.velocity = Vector3.ClampMagnitude (item.attachedRigidbody.velocity, 5);
  156. GravityTarget = null;
  157. playerController.slowed = false;
  158. playerController.movementLock = false;
  159. playerController.grip = normGrip;
  160. playerController.cameraSpeedX = 250.0f;
  161. playerController.cameraSpeedY = 120.0f;
  162. }
  163. }
  164. private void pullItem(float speed){
  165. if (!Input.GetButton ("Quick Push"))
  166. return;
  167. float step = maxPullSpeed * 0.5f * Time.deltaTime;
  168. Vector3 maxPull;
  169. maxPull = camera.transform.position + (camera.transform.rotation * Vector3.forward);
  170. if ((Vector3.Distance (gravityWell.transform.position, camera.transform.position) > minDistance && Vector3.Distance (gravityWell.transform.position, camera.transform.position) < magnetRange) || maxPullSpeed<1) {
  171. gravityWell.transform.position = Vector3.MoveTowards (gravityWell.transform.position, maxPull, step);
  172. if ((Vector3.Distance (gravityWell.transform.position, camera.transform.position) > magnetRange) && !topScreen)
  173. dropItem (GravityTarget);
  174. }
  175. }
  176. private void rotateInput(Collider item){
  177. if (item != null) {
  178. if (Input.GetButtonDown(L_BUTTON_INPUT))
  179. StartCoroutine (rotateItem(item,new Vector3 (0,90,0),0.3f));
  180. if (Input.GetButtonDown(R_BUTTON_INPUT))
  181. StartCoroutine (rotateItem(item,new Vector3 (90,0,0),0.3f));
  182. }
  183. }
  184. IEnumerator rotateItem (Collider item, Vector3 byAngles, float inTime){
  185. Quaternion startAngle = item.transform.rotation;
  186. Quaternion endAngle = Quaternion.Euler (item.transform.eulerAngles + byAngles);
  187. for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
  188. item.transform.rotation = Quaternion.Lerp(startAngle,endAngle,i);
  189. yield return null;
  190. }
  191. }
  192. public void cameraRotateTest(Collider item){
  193. if (item != null) {
  194. //Debug.Log ("distance test: " + Vector3.Distance (gravityWell.transform.position, camera.transform.position));
  195. if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) <= closeRange) {
  196. playerController.cameraSpeedX = 250.0f;
  197. playerController.cameraSpeedY = 120.0f;
  198. } else {
  199. playerController.cameraSpeedX = 0.0f;
  200. playerController.cameraSpeedY = 0.0f;
  201. }
  202. }
  203. }
  204. public void updateColors(Collider target){
  205. if (target != null && target.tag == "moveable") {
  206. lastTargetRenderer = target.GetComponent<Renderer> ();
  207. if (lastTargetRenderer.material.color != Color.white) {
  208. lastTargetColour = lastTargetRenderer.material.color;
  209. lastTargetRenderer.material.color = Color.white;
  210. }
  211. } else {
  212. if (lastTargetRenderer != null)
  213. lastTargetRenderer.material.color = lastTargetColour;
  214. }
  215. }
  216. private bool getAxisDown (string axis){
  217. if( Input.GetAxisRaw(axis) != 0)
  218. {
  219. if(axisDown == false)
  220. {
  221. axisDown = true;
  222. return true;
  223. }
  224. }
  225. if( Input.GetAxisRaw(axis) == 0)
  226. {
  227. axisDown = false;
  228. }
  229. return false;
  230. }
  231. }