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.

253 lines
6.6 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 string L_TRIGGER_INPUT;
  9. public string R_TRIGGER_INPUT;
  10. public string L_BUTTON_INPUT;
  11. public string R_BUTTON_INPUT;
  12. private bool L_Trigger_Down;
  13. public bool topScreen = true;
  14. private float playerScreen = 2;
  15. public GameObject gravityWell;
  16. private Collider GravityTarget;
  17. private Vector3 rayHitPoint;
  18. private Vector3 targetOffset;
  19. public float objectSpeed;
  20. public float magnetRange = 20;
  21. public float maxPullSpeed = 30;
  22. public float rotateSpeed;
  23. public float minDistance = 3.0f;
  24. public float impulsePower = 50.0f;
  25. private float maxWalkSpeed;
  26. private Renderer lastTargetRenderer;
  27. private Color lastTargetColour;
  28. // Use this for initialization
  29. void Start () {
  30. if (topScreen)
  31. playerScreen = 2;
  32. else
  33. playerScreen = -1;
  34. maxWalkSpeed = playerController.movementSpeed;
  35. }
  36. // Update is called once per frame
  37. void Update () {
  38. float triggerL = Input.GetAxis(L_TRIGGER_INPUT);
  39. float triggerR = Input.GetAxis (R_TRIGGER_INPUT);
  40. Collider target = testItem ();
  41. crossHair.GetComponent<RawImage> ().color = Color.white;
  42. if (target != null){
  43. if (target.GetComponent<Collider>().tag == "moveable"){
  44. Debug.Log("Moveable Item");
  45. crossHair.GetComponent<RawImage>().color = Color.red;
  46. if (Vector3.Distance (rayHitPoint, camera.transform.position) <= minDistance) {
  47. crossHair.GetComponent<RawImage>().color = Color.blue;
  48. }
  49. }
  50. }
  51. updateColors (target);
  52. if (triggerL > 0) {
  53. if (!L_Trigger_Down) {
  54. pickUpItem (target);
  55. L_Trigger_Down = true;
  56. }
  57. } else {
  58. L_Trigger_Down = false;
  59. dropItem(GravityTarget);
  60. }
  61. impulsePush (target);
  62. moveItem (GravityTarget);
  63. pullItem (triggerR);
  64. rotateInput (GravityTarget);
  65. cameraRotateTest (GravityTarget);
  66. }
  67. private Collider testItem(){
  68. RaycastHit hit;
  69. //Vector3 rayDirection = camera.transform.rotation * Vector3.forward;
  70. //Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2 + (Screen.height/6 * playerScreen), 0));
  71. Ray ray = camera.ScreenPointToRay(crossHair.transform.position);
  72. Debug.DrawRay (ray.origin, ray.direction*magnetRange, Color.green);
  73. if (Physics.Raycast (ray, out hit, magnetRange)) {
  74. if (hit.collider.tag == "moveable"){
  75. Debug.DrawRay (ray.origin,ray.direction*magnetRange, Color.red);
  76. rayHitPoint = hit.point;
  77. }
  78. }
  79. return hit.collider;
  80. }
  81. private void pickUpItem(Collider item){
  82. if (item != null) {
  83. if (item.tag == "moveable"){
  84. item.attachedRigidbody.useGravity = false;
  85. item.attachedRigidbody.drag = 3.0f;
  86. item.attachedRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
  87. //camera.transform.LookAt(item.transform.position);
  88. //playerController.cameraX = camera.transform.eulerAngles.x;
  89. //playerController.cameraY = camera.transform.eulerAngles.y;
  90. gravityWell.transform.position = rayHitPoint;
  91. GravityTarget = item;
  92. targetOffset = GravityTarget.transform.position - rayHitPoint + (Vector3.up * 0.05f);
  93. playerController.movementSpeed = 200.0f;
  94. if (!topScreen){
  95. playerController.cameraSpeedX = 0.0f;
  96. playerController.cameraSpeedY = 0.0f;
  97. }
  98. }
  99. }
  100. }
  101. private void impulsePush (Collider item){
  102. if (item != null) {
  103. if (item.tag == "moveable" && Input.GetButtonDown("Quick Push")){
  104. Vector3 direction = (transform.position - item.transform.position).normalized;
  105. item.attachedRigidbody.AddForce(direction * impulsePower * maxPullSpeed,ForceMode.Impulse);
  106. }
  107. }
  108. }
  109. private void moveItem(Collider item){
  110. if (item != null) {
  111. float step = objectSpeed * Time.deltaTime;
  112. Vector3 direction = gravityWell.transform.position - item.transform.position + targetOffset;
  113. direction = Vector3.ClampMagnitude(direction,1.0f);
  114. item.attachedRigidbody.AddForce(direction * objectSpeed * Time.deltaTime);
  115. }
  116. }
  117. private void dropItem(Collider item){
  118. if (item != null) {
  119. item.attachedRigidbody.useGravity = true;
  120. item.attachedRigidbody.drag = 0.0f;
  121. item.attachedRigidbody.constraints = RigidbodyConstraints.None;
  122. if(topScreen)
  123. item.attachedRigidbody.velocity = Vector3.ClampMagnitude (item.attachedRigidbody.velocity, 5);
  124. GravityTarget = null;
  125. playerController.movementSpeed = maxWalkSpeed;
  126. playerController.cameraSpeedX = 250.0f;
  127. playerController.cameraSpeedY = 120.0f;
  128. }
  129. }
  130. private void pullItem(float speed){
  131. float step = maxPullSpeed * speed * Time.deltaTime;
  132. Vector3 maxPull;
  133. maxPull = camera.transform.position + (camera.transform.rotation * Vector3.forward);
  134. if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) > minDistance || maxPullSpeed<1) {
  135. gravityWell.transform.position = Vector3.MoveTowards (gravityWell.transform.position, maxPull, step);
  136. if ((Vector3.Distance (gravityWell.transform.position, camera.transform.position) > magnetRange) && !topScreen)
  137. dropItem (GravityTarget);
  138. }
  139. }
  140. private void rotateInput(Collider item){
  141. if (item != null) {
  142. if (Input.GetButtonDown(L_BUTTON_INPUT))
  143. StartCoroutine (rotateItem(item,new Vector3 (0,90,0),0.3f));
  144. if (Input.GetButtonDown(R_BUTTON_INPUT))
  145. StartCoroutine (rotateItem(item,new Vector3 (90,0,0),0.3f));
  146. }
  147. }
  148. IEnumerator rotateItem (Collider item, Vector3 byAngles, float inTime){
  149. Quaternion startAngle = item.transform.rotation;
  150. Quaternion endAngle = Quaternion.Euler (item.transform.eulerAngles + byAngles);
  151. for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
  152. item.transform.rotation = Quaternion.Lerp(startAngle,endAngle,i);
  153. yield return null;
  154. }
  155. }
  156. public void cameraRotateTest(Collider item){
  157. if (item != null) {
  158. Debug.Log ("distance test: " + Vector3.Distance (gravityWell.transform.position, camera.transform.position));
  159. if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) <= minDistance) {
  160. playerController.cameraSpeedX = 250.0f;
  161. playerController.cameraSpeedY = 120.0f;
  162. } else {
  163. playerController.cameraSpeedX = 0.0f;
  164. playerController.cameraSpeedY = 0.0f;
  165. }
  166. }
  167. }
  168. public void updateColors(Collider target){
  169. if (target != null && target.tag == "moveable") {
  170. lastTargetRenderer = target.GetComponent<Renderer> ();
  171. if (lastTargetRenderer.material.color != Color.white) {
  172. lastTargetColour = lastTargetRenderer.material.color;
  173. lastTargetRenderer.material.color = Color.white;
  174. }
  175. } else {
  176. if (lastTargetRenderer != null)
  177. lastTargetRenderer.material.color = lastTargetColour;
  178. }
  179. }
  180. }