Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Raycast : MonoBehaviour {
  5. public LayerMask layerMask;
  6. public float force = 150f;
  7. public LineRenderer linerenderer;
  8. public float counter = 0;
  9. public bool drawn = false;
  10.  
  11. private Object box;
  12. // Use this for initialization
  13. void Start () {
  14.  
  15. }
  16.  
  17.  
  18. void OnMouseDown()
  19. {
  20.  
  21. }
  22.  
  23. void DropItem()
  24. {
  25. Debug.Log("DROPINN");
  26. linerenderer.SetPosition (0, Vector3.zero);
  27. linerenderer.SetPosition (1, Vector3.zero);
  28. if(draggedItem != null)
  29. draggedItem.isKinematic = false;
  30. dragging = false;
  31. counter = 0;
  32. drawn = false;
  33. }
  34.  
  35. bool dragging = false;
  36. Rigidbody2D draggedItem;
  37.  
  38. // Update is called once per frame
  39. void Update () {
  40. Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
  41.  
  42. if (Input.GetMouseButtonDown(0))
  43. {
  44. targetPos = transform.InverseTransformPoint(targetPos);
  45. Debug.DrawRay(this.transform.position, targetPos);
  46.  
  47. if(transform.localScale.x <0)
  48. targetPos.x = targetPos.x * -1;
  49.  
  50.  
  51. RaycastHit2D hit = Physics2D.Raycast(transform.position, targetPos, 500f, layerMask);
  52. if (hit != null && hit.transform != null && hit.transform.gameObject.tag == "Pickable")
  53. {
  54.  
  55. GameObject tmp = hit.transform.gameObject;
  56. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  57. RaycastHit2D hit2 = Physics2D.Raycast(ray.origin, ray.direction, 500f);
  58. if (hit2.transform != null && tmp == hit2.transform.gameObject)
  59. {
  60. dragging = true;
  61. //hit.transform.rigidbody2D.isKinematic = true;
  62. draggedItem = hit.transform.rigidbody2D;
  63. }
  64. }
  65. }
  66.  
  67. else if (dragging && Input.GetMouseButton(0))
  68. {
  69. RaycastHit2D hit = Physics2D.Raycast(this.transform.position, draggedItem.transform.position - this.transform.position, Mathf.Infinity, layerMask);
  70. Debug.DrawRay(this.transform.position, draggedItem.transform.position - this.transform.position);
  71. linerenderer.SetPosition (0, this.transform.position);
  72. //linerenderer.SetPosition (1, hit.transform.position);
  73. float dist = Vector2.Distance(this.transform.position,hit.transform.position);
  74. Debug.Log(dist.ToString());
  75. if(counter<dist && drawn==false)
  76. {
  77. counter += .1f/1;
  78. float x = Mathf.Lerp(0,dist,counter);
  79. Vector3 pointA = this.transform.position;
  80. Vector3 pointB = hit.transform.position;
  81.  
  82. Vector2 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
  83. linerenderer.SetPosition(1, pointAlongLine);
  84. }
  85. else
  86. {
  87. drawn=true;
  88. linerenderer.SetPosition (1, hit.transform.position);
  89. }
  90.  
  91. Vector2 newPos = new Vector2(draggedItem.position.x - targetPos.x, draggedItem.position.y - targetPos.y)*-1;
  92. draggedItem.velocity = Vector2.zero;
  93. draggedItem.AddForce(newPos * force);
  94.  
  95. if (hit != null && hit.transform.gameObject.tag != "Pickable")
  96. {
  97. DropItem();
  98.  
  99. }
  100.  
  101.  
  102. }
  103.  
  104. if (dragging && Input.GetMouseButtonUp(0))
  105. {
  106. DropItem();
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement