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.

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