0

I need help how to fix dragging object. When I move camera to the right (also with image/sprite object) and I drag object, it appears on previous position. If I change Render Mode to screen space - overlay, its working but only with images not with sprites. When I move camera image object will move with camera (what I want) but sprites don't thats why I changed render mode to screen space - camera. I need move camera with sprite object and be able to drag that object.

Dragable object has following code:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Dragable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {

public Transform parentToReturnTo = null;
public enum Slot { TYPE1, TYPE2 };
public Slot typeOfItem = Slot.INVENTORY;
GameObject placeholder = null;
Animator animDrop;

void Start()
{
    animDrop = GetComponentInChildren<Animator>();
}

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;
    this.transform.SetParent(this.transform.parent.parent);
    GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
    this.transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
    Debug.Log("OnEndDrag");
    this.transform.SetParent(parentToReturnTo);
    this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
    GetComponent<CanvasGroup>().blocksRaycasts = true;
    Destroy(placeholder);
    animDrop.SetBool("OnDropAnimation", true);
}
}

To move camera I have following script

using UnityEngine;
using UnityEngine.EventSystems;

public class TouchController : MonoBehaviour
{
float touchStart = 0f;
Vector3 cameraDestination;
public float cameraSpeed = 0.1f;
public float maxPosX = 2900f;
public float minPosX = -2900f;
public bool DragUI = false;

public static bool IsPointerOverGameObject()
{
    if (EventSystem.current.IsPointerOverGameObject())
        return true;

    if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    {
        if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
            return true;
    }
    return false;
}
void Start()
{
    cameraDestination = Camera.main.transform.position;
}
void Update()
{
    if (IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
    {
        DragUI = true;
    }
    if (Input.GetMouseButtonDown(0))
    {
        touchStart = Input.mousePosition.x;
    }
    if (Input.GetMouseButtonUp(0))
    {
        float delta = Input.mousePosition.x - touchStart;
        if (delta < -50f && !DragUI)
        {
            if (cameraDestination.x < maxPosX)
            {
                cameraDestination = new Vector3(Camera.main.transform.position.x + 1600,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
            }
        }
        else if (delta > 50f && !DragUI)
        {
            if (cameraDestination.x > minPosX)
            {
                cameraDestination = new Vector3(Camera.main.transform.position.x - 1600,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
                Debug.Log(cameraDestination.x + " cam dest x left");
            }
        }
        else { DragUI = false;}
    }
    if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f && !DragUI)
    {
        if (Camera.main.transform.position.x > cameraDestination.x)
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - cameraSpeed,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
            DragUI = false;
        }
        else
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
            DragUI = false;
        }
    }
}
}

enter image description here enter image description here

vb381
  • 181
  • 1
  • 3
  • 14
  • If you want to add images to your question please use StackOverflow's built-in method instead of posting external URLs. If you want help fixing a code than provide your current code please. – derHugo Jan 15 '19 at 06:37
  • sry, I updated my question – vb381 Jan 15 '19 at 18:41

1 Answers1

0

Sprites are Objects that "live" in the Worldspace and so you need to change your canvas to act in World space to mix images and Sprites. but it seems that the way you are going is not the best for your problem because it looks a little bit weird from your description.

Vampirasu
  • 202
  • 1
  • 12