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 maxWalkSpeed;
  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. maxWalkSpeed = playerController.movementSpeed;
  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.movementSpeed = 200.0f;
  98. if (!topScreen){
  99. playerController.cameraSpeedX = 0.0f;
  100. playerController.cameraSpeedY = 0.0f;
  101. }
  102. }
  103. }
  104. }
  105. private void impulsePush (Collider item){
  106. if (item != null) {
  107. if (item.tag == "moveable" && Input.GetButtonDown("Quick Push")){
  108. Vector3 direction = (transform.position - item.transform.position).normalized;
  109. item.attachedRigidbody.AddForce(direction * impulsePower * maxPullSpeed* item.attachedRigidbody.mass,ForceMode.Impulse );
  110. }
  111. }
  112. }
  113. private void moveItem(Collider item){
  114. if (item != null) {
  115. float step = objectSpeed * Time.deltaTime;
  116. Vector3 direction = gravityWell.transform.position - item.transform.position + targetOffset;
  117. direction = Vector3.ClampMagnitude(direction,1.0f);
  118. item.attachedRigidbody.AddForce(direction * objectSpeed * Time.deltaTime);
  119. }
  120. }
  121. private void dropItem(Collider item){
  122. if (item != null) {
  123. item.attachedRigidbody.useGravity = originalGravity;
  124. item.attachedRigidbody.drag = 0.0f;
  125. item.attachedRigidbody.constraints = originalConstrants;
  126. if(topScreen)
  127. item.attachedRigidbody.velocity = Vector3.ClampMagnitude (item.attachedRigidbody.velocity, 5);
  128. GravityTarget = null;
  129. playerController.movementSpeed = maxWalkSpeed;
  130. playerController.cameraSpeedX = 250.0f;
  131. playerController.cameraSpeedY = 120.0f;
  132. }
  133. }
  134. private void pullItem(float speed){
  135. float step = maxPullSpeed * speed * Time.deltaTime;
  136. Vector3 maxPull;
  137. maxPull = camera.transform.position + (camera.transform.rotation * Vector3.forward);
  138. if ((Vector3.Distance (gravityWell.transform.position, camera.transform.position) > minDistance && Vector3.Distance (gravityWell.transform.position, camera.transform.position) < magnetRange) || maxPullSpeed<1) {
  139. gravityWell.transform.position = Vector3.MoveTowards (gravityWell.transform.position, maxPull, step);
  140. if ((Vector3.Distance (gravityWell.transform.position, camera.transform.position) > magnetRange) && !topScreen)
  141. dropItem (GravityTarget);
  142. }
  143. }
  144. private void rotateInput(Collider item){
  145. if (item != null) {
  146. if (Input.GetButtonDown(L_BUTTON_INPUT))
  147. StartCoroutine (rotateItem(item,new Vector3 (0,90,0),0.3f));
  148. if (Input.GetButtonDown(R_BUTTON_INPUT))
  149. StartCoroutine (rotateItem(item,new Vector3 (90,0,0),0.3f));
  150. }
  151. }
  152. IEnumerator rotateItem (Collider item, Vector3 byAngles, float inTime){
  153. Quaternion startAngle = item.transform.rotation;
  154. Quaternion endAngle = Quaternion.Euler (item.transform.eulerAngles + byAngles);
  155. for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
  156. item.transform.rotation = Quaternion.Lerp(startAngle,endAngle,i);
  157. yield return null;
  158. }
  159. }
  160. public void cameraRotateTest(Collider item){
  161. if (item != null) {
  162. //Debug.Log ("distance test: " + Vector3.Distance (gravityWell.transform.position, camera.transform.position));
  163. if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) <= minDistance) {
  164. playerController.cameraSpeedX = 250.0f;
  165. playerController.cameraSpeedY = 120.0f;
  166. } else {
  167. playerController.cameraSpeedX = 0.0f;
  168. playerController.cameraSpeedY = 0.0f;
  169. }
  170. }
  171. }
  172. public void updateColors(Collider target){
  173. if (target != null && target.tag == "moveable") {
  174. lastTargetRenderer = target.GetComponent<Renderer> ();
  175. if (lastTargetRenderer.material.color != Color.white) {
  176. lastTargetColour = lastTargetRenderer.material.color;
  177. lastTargetRenderer.material.color = Color.white;
  178. }
  179. } else {
  180. if (lastTargetRenderer != null)
  181. lastTargetRenderer.material.color = lastTargetColour;
  182. }
  183. }
  184. }