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.

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