Hi have start to build a new game and i need to buid an arrow were is done by the mouse button down and drag like this example i find on youtube.
Original link
i have try to do it and i all much archive what i need but i set in my example to the input.mousebutton.x ,y, z but i liked to change the coords to my first game object and then finish on the target.
this is what i have get until now
code bellow
This is my code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public Transform parentToReturnTo = null;
public Transform placeholderParent = null;
public GameObject cadr1;
public GameObject cadr2;
public GameObject cadr3;
public GameObject cadr4;
public GameObject enemycard1;
public GameObject enemycard2;
public GameObject enemycard3;
public GameObject enemycard4;
public Material mater1;
public float linewith;
public float depth = 5;
public GameObject mira;
public LineRenderer line;
GameObject placeholder = null;
public void OnBeginDrag(PointerEventData eventData) {
Debug.Log ("OnBeginDrag");
placeholder = new GameObject();
placeholder.transform.SetParent( this.transform.parent );
LayoutElement le = placeholder.AddComponent<LayoutElement>();
le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
le.flexibleWidth = 0;
le.flexibleHeight = 0;
placeholder.transform.SetSiblingIndex( this.transform.GetSiblingIndex() );
parentToReturnTo = this.transform.parent;
placeholderParent = parentToReturnTo;
this.transform.SetParent( this.transform.parent.parent );
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData) {
//Debug.Log ("OnDrag");
placeholder.transform.position = Input.mousePosition;
this.transform.position = Vector3.Lerp(this.transform.position, eventData.position, 0.01f);
mira.SetActive (true);
mira.transform.position = Input.mousePosition;
line = transform.GetComponent<LineRenderer> ();
line.SetVertexCount (20);
//line.SetPosition (0, new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
line.SetPosition (0, new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
if(placeholder.transform.parent != placeholderParent)
placeholder.transform.SetParent(placeholderParent);
int newSiblingIndex = placeholderParent.childCount;
for(int i=0; i < placeholderParent.childCount; i++) {
if(this.transform.position.x < placeholderParent.GetChild(i).position.x) {
newSiblingIndex = i;
if(placeholder.transform.GetSiblingIndex() < newSiblingIndex)
newSiblingIndex--;
break;
}
}
placeholder.transform.SetSiblingIndex(newSiblingIndex);
}
public void OnEndDrag(PointerEventData eventData) {
Debug.Log ("OnEndDrag");
this.transform.SetParent( parentToReturnTo );
this.transform.SetSiblingIndex( placeholder.transform.GetSiblingIndex() );
GetComponent<CanvasGroup>().blocksRaycasts = true;
Destroy(placeholder);
}
void Start()
{
mira.SetActive (false);
}
void Update()
{
}
}
Here is the screenshot i have take from it with the example