Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Raycast : MonoBehaviour {
- public LayerMask layerMask;
- public float force = 150f;
- public LineRenderer linerenderer;
- public float counter = 0;
- public bool drawn = false;
- private Object box;
- // Use this for initialization
- void Start () {
- }
- void OnMouseDown()
- {
- }
- void DropItem()
- {
- Debug.Log("DROPINN");
- linerenderer.SetPosition (0, Vector3.zero);
- linerenderer.SetPosition (1, Vector3.zero);
- if(draggedItem != null)
- draggedItem.isKinematic = false;
- dragging = false;
- counter = 0;
- drawn = false;
- }
- bool dragging = false;
- Rigidbody2D draggedItem;
- // Update is called once per frame
- void Update () {
- Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
- if (Input.GetMouseButtonDown(0))
- {
- targetPos = transform.InverseTransformPoint(targetPos);
- Debug.DrawRay(this.transform.position, targetPos);
- if(transform.localScale.x <0)
- targetPos.x = targetPos.x * -1;
- RaycastHit2D hit = Physics2D.Raycast(transform.position, targetPos, 500f, layerMask);
- if (hit != null && hit.transform != null && hit.transform.gameObject.tag == "Pickable")
- {
- GameObject tmp = hit.transform.gameObject;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit2D hit2 = Physics2D.Raycast(ray.origin, ray.direction, 500f);
- if (hit2.transform != null && tmp == hit2.transform.gameObject)
- {
- dragging = true;
- //hit.transform.rigidbody2D.isKinematic = true;
- draggedItem = hit.transform.rigidbody2D;
- }
- }
- }
- else if (dragging && Input.GetMouseButton(0))
- {
- RaycastHit2D hit = Physics2D.Raycast(this.transform.position, draggedItem.transform.position - this.transform.position, Mathf.Infinity, layerMask);
- Debug.DrawRay(this.transform.position, draggedItem.transform.position - this.transform.position);
- linerenderer.SetPosition (0, this.transform.position);
- //linerenderer.SetPosition (1, hit.transform.position);
- float dist = Vector2.Distance(this.transform.position,hit.transform.position);
- Debug.Log(dist.ToString());
- if(counter<dist && drawn==false)
- {
- counter += .1f/1;
- float x = Mathf.Lerp(0,dist,counter);
- Vector3 pointA = this.transform.position;
- Vector3 pointB = hit.transform.position;
- Vector2 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
- linerenderer.SetPosition(1, pointAlongLine);
- }
- else
- {
- drawn=true;
- linerenderer.SetPosition (1, hit.transform.position);
- }
- Vector2 newPos = new Vector2(draggedItem.position.x - targetPos.x, draggedItem.position.y - targetPos.y)*-1;
- draggedItem.velocity = Vector2.zero;
- draggedItem.AddForce(newPos * force);
- if (hit != null && hit.transform.gameObject.tag != "Pickable")
- {
- DropItem();
- }
- }
- if (dragging && Input.GetMouseButtonUp(0))
- {
- DropItem();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement