Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kenmarold · Aug 13, 2015 at 07:55 AM · c#unity 5drag

Object drag not moving at the same rate as the pointer

I'm trying to implement a drag and drop system but I'm having a problem with the drag rate at the moment. Currently when I drag an object it only moves a tiny fraction of the amount that my pointer actually moves. How do I fix this?

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityEngine.EventSystems;
 using System.Collections.Generic;
 
 public class DragHandling : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
 {
     public float partScale;
 
     [HideInInspector] public Transform placeholderParent = null;
     [HideInInspector] public Transform parentToReturnTo = null;
     [HideInInspector] public GameObject trashCan; 
     [HideInInspector] public GameObject partsPanel;
     [HideInInspector] public GameObject partsWindow;
     [HideInInspector] public GameObject buildBoard;
     [HideInInspector] public int orderIndex;
 
     GameObject placeholder = null;
     GameObject dragLayer;
     Vector3 buildPanelScale;
     Vector3 partsPanelScale = new Vector3(1.0f, 1.0f, 1.0f);
     Vector3 startPosition;
 
     private Vector3 offset = Vector3.zero;
 
     void Start ()
     {
         dragLayer = GameObject.FindGameObjectWithTag("DragLayer");
         buildBoard = GameObject.FindGameObjectWithTag("Board");
         partsPanel = GameObject.FindGameObjectWithTag("Parts");
         partsWindow = GameObject.FindGameObjectWithTag("PartsWindow");
         trashCan = GameObject.FindGameObjectWithTag("Trash");
     }
 
     #region IPointerClickHandler implementation
 
     public void OnPointerClick (PointerEventData eventData)
     {
         if(transform.parent.gameObject == buildBoard)
             transform.SetAsLastSibling();
     }
 
     #endregion
 
     #region IBeginDragHandler implementation
 
     public void OnBeginDrag (PointerEventData eventData)
     {
         Vector3 worldPos = Camera.main.ScreenToWorldPoint(eventData.position);
         worldPos.z = transform.position.z;
         offset = worldPos - transform.position;
 
         // startPosition = transform.position;
 
         // create placeholder gap and hold correct position in layout
         orderIndex = transform.GetSiblingIndex();
 
         placeholder = new GameObject();
         placeholder.transform.SetParent(transform.parent);
         placeholder.transform.SetSiblingIndex(transform.GetSiblingIndex());
         parentToReturnTo = transform.parent;                                    // store original parent location
         placeholderParent = parentToReturnTo;                                    // set placeholder gameobject transform
         GetComponent<CanvasGroup>().blocksRaycasts = false;                        // turn off image raycasting when dragging image in order to see what's behind the image            
     }
 
     #endregion
 
     #region IDragHandler implementation
     
     public void OnDrag (PointerEventData eventData)
     {
         Vector3 worldPos = Camera.main.ScreenToWorldPoint(eventData.position);
         worldPos.z = transform.position.z;
         worldPos = worldPos - offset;
         transform.position = worldPos;
 
         // Vector3 mousePosition = new Vector3(eventData.position.x, eventData.position.y, 0);
         // transform.position = Input.mousePosition;                                            // set object coordinates to mouse coordinates
 
         if(transform.parent.gameObject == partsPanel)
             transform.SetParent(dragLayer.transform);                                        // pop object to draglayer to move object out of parts Panel
         if(transform.parent.gameObject == buildBoard)
             transform.SetParent(dragLayer.transform);
     }
     
     #endregion
 
     #region IEndDragHandler implementation
 
     public void OnEndDrag (PointerEventData eventData)
     {
         offset = Vector3.zero;
 
         transform.SetParent(parentToReturnTo);                                    // Snaps object back to orginal parent if dropped outside of a dropzone
         transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());        // Returns card back to placeholder location
         GetComponent<CanvasGroup>().blocksRaycasts = true;                        // turn Raycast back on
         Destroy(placeholder);                                                    // kill the placeholder if object hits a drop zone or returns to parts panel
 
         if(transform.parent.gameObject == buildBoard)
         {
             buildPanelScale = new Vector3(partScale, partScale, partScale);
             transform.localScale = buildPanelScale;
             transform.SetAsLastSibling();                                        // always place last piece on top
         }
         if(transform.parent.gameObject == partsPanel)
             transform.localScale = partsPanelScale;
             transform.SetSiblingIndex(orderIndex);
     }
 
     #endregion
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by t_w · Sep 13, 2015 at 10:16 AM

Maybe you can try change your Projection to Orthographic in your main camera.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problem with jumping in fps game 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Dragable UI Around the Player Using a Radius 1 Answer

Sluggish relative drag 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges