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.

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