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.

203 lines
5.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 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. public float objectSpeed;
  19. public float magnetRange = 20;
  20. public float maxPullSpeed = 30;
  21. public float rotateSpeed;
  22. public float minDistance = 3.0f;
  23. // Use this for initialization
  24. void Start () {
  25. if (topScreen)
  26. playerScreen = 2;
  27. else
  28. playerScreen = -1;
  29. }
  30. // Update is called once per frame
  31. void Update () {
  32. float triggerL = Input.GetAxis(L_TRIGGER_INPUT);
  33. float triggerR = Input.GetAxis (R_TRIGGER_INPUT);
  34. Collider target = testItem ();
  35. crossHair.GetComponent<RawImage> ().color = Color.white;
  36. if (target != null){
  37. if (target.GetComponent<Collider>().tag == "moveable"){
  38. Debug.Log("Moveable Item");
  39. crossHair.GetComponent<RawImage>().color = Color.red;
  40. }
  41. }
  42. if (triggerL > 0) {
  43. if (!L_Trigger_Down) {
  44. pickUpItem (target);
  45. L_Trigger_Down = true;
  46. }
  47. } else {
  48. L_Trigger_Down = false;
  49. dropItem(GravityTarget);
  50. }
  51. moveItem (GravityTarget);
  52. pullItem (triggerR);
  53. rotateInput (GravityTarget);
  54. cameraRotateTest (GravityTarget);
  55. }
  56. private Collider testItem(){
  57. RaycastHit hit;
  58. //Vector3 rayDirection = camera.transform.rotation * Vector3.forward;
  59. Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2 + (Screen.height/6 * playerScreen), 0));
  60. Debug.DrawRay (ray.origin, ray.direction*magnetRange, Color.green);
  61. if (Physics.Raycast (ray, out hit, magnetRange)) {
  62. if (hit.collider.tag == "moveable"){
  63. Debug.DrawRay (ray.origin,ray.direction*magnetRange, Color.red);
  64. rayHitPoint = hit.point;
  65. }
  66. }
  67. return hit.collider;
  68. }
  69. private void pickUpItem(Collider item){
  70. if (item != null) {
  71. if (item.tag == "moveable"){
  72. item.attachedRigidbody.useGravity = false;
  73. item.attachedRigidbody.drag = 3.0f;
  74. item.attachedRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
  75. gravityWell.transform.position = rayHitPoint;
  76. GravityTarget = item;
  77. playerController.movementSpeed = 200.0f;
  78. if (!topScreen){
  79. playerController.cameraSpeedX = 0.0f;
  80. playerController.cameraSpeedY = 0.0f;
  81. }
  82. }
  83. }
  84. }
  85. private void moveItem(Collider item){
  86. if (item != null) {
  87. float step = objectSpeed * Time.deltaTime;
  88. Vector3 direction = gravityWell.transform.position - item.transform.position;
  89. direction = Vector3.ClampMagnitude(direction,1.0f);
  90. item.attachedRigidbody.AddForce(direction * objectSpeed * Time.deltaTime);
  91. }
  92. }
  93. private void dropItem(Collider item){
  94. if (item != null) {
  95. item.attachedRigidbody.useGravity = true;
  96. item.attachedRigidbody.drag = 0.0f;
  97. item.attachedRigidbody.constraints = RigidbodyConstraints.None;
  98. if(topScreen)
  99. item.attachedRigidbody.velocity = Vector3.ClampMagnitude (item.attachedRigidbody.velocity, 5);
  100. GravityTarget = null;
  101. playerController.movementSpeed = 5000.0f;
  102. playerController.cameraSpeedX = 250.0f;
  103. playerController.cameraSpeedY = 120.0f;
  104. }
  105. }
  106. private void pullItem(float speed){
  107. float step = maxPullSpeed * speed * Time.deltaTime;
  108. Vector3 maxPull;
  109. maxPull = camera.transform.position + (camera.transform.rotation * Vector3.forward);
  110. if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) > minDistance || maxPullSpeed<1) {
  111. gravityWell.transform.position = Vector3.MoveTowards (gravityWell.transform.position, maxPull, step);
  112. if ((Vector3.Distance (gravityWell.transform.position, camera.transform.position) > magnetRange) && !topScreen)
  113. dropItem (GravityTarget);
  114. }
  115. }
  116. private void rotateInput(Collider item){
  117. if (item != null) {
  118. if (Input.GetButtonDown(L_BUTTON_INPUT))
  119. StartCoroutine (rotateItem(item,new Vector3 (0,90,0),0.3f));
  120. if (Input.GetButtonDown(R_BUTTON_INPUT))
  121. StartCoroutine (rotateItem(item,new Vector3 (90,0,0),0.3f));
  122. }
  123. }
  124. IEnumerator rotateItem (Collider item, Vector3 byAngles, float inTime){
  125. Quaternion startAngle = item.transform.rotation;
  126. Quaternion endAngle = Quaternion.Euler (item.transform.eulerAngles + byAngles);
  127. for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
  128. item.transform.rotation = Quaternion.Lerp(startAngle,endAngle,i);
  129. yield return null;
  130. }
  131. }
  132. public void cameraRotateTest(Collider item){
  133. if (item != null) {
  134. Debug.Log ("distance test: " + Vector3.Distance (gravityWell.transform.position, camera.transform.position));
  135. if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) <= minDistance) {
  136. playerController.cameraSpeedX = 250.0f;
  137. playerController.cameraSpeedY = 120.0f;
  138. } else {
  139. playerController.cameraSpeedX = 0.0f;
  140. playerController.cameraSpeedY = 0.0f;
  141. }
  142. }
  143. }
  144. }