Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Nov 17, 2015 at 10:52 AM by CrispyArrow for the following reason:

It turned out that the reason the dragging did not work is because most of the UI objects were not actual UI elements, but rather sprite renderers. Make sure when you make a canvas that all child objects are UI objects

avatar image
0
Question by CrispyArrow · Nov 10, 2015 at 09:38 AM · positioningdrag-and-drop

Drag & drop start position on a perspective camera?

I have a script that allows me to drap and release an image on my canvas. the canvas is using a perspective camera as it's render camera. I have seen multiple tutorials about Drag and drop so this code is made specifically for that. However, those tutorials do not mention how to do this for perspective cameras.

What I want to do is this: when you start dragging the image, it will create a start position of that object. when you top dragging the object, it will use that start position to return the image to it's original position before the dragging started. You can see on the code that I specifically pointed out which code needs to be changed. the code works, but not on perspective cameras, in which, it will cause the image to be moved to a position relevant to the world space, rather than the camera.

What can I change in my code to make this work properly?

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
     public static GameObject itemBeingDragged;
     Ray startPosition;
     Vector2 point;
     public float distance;
 
     #region IBeginDragHandler implementation
 
     public void OnBeginDrag (PointerEventData eventData)
     {
         itemBeingDragged = gameObject;
 
     /*-------------------->>>*/    startPosition = transform.position;
 
         distance = Vector2.Distance(transform.position,    Camera.main.transform.position);
     }
 
     #endregion
 
 
     #region IDragHandler implementation
     public void OnDrag (PointerEventData eventData)
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Vector3 rayPoint = ray.GetPoint(distance);
         transform.position = rayPoint;
 
     }
     #endregion
 
 
     #region IEndDragHandler implementation
 
     public void OnEndDrag (PointerEventData eventData)
     {
         itemBeingDragged = null;
 
         /*-------------------->>>*/ transform.position = startPosition;
     }
 
     #endregion
 }
 


If there's something that is unclear, or if you need additional information, please let me know.

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting UI element position in physical units during runtime. 0 Answers

Change position of AdMob banner when opening new scene 3 Answers

Respawn Ball to specific spawnpoint after death 1 Answer

OnTriggerEnter and position problem 0 Answers

How to get screen relative position in multi-display environment? 0 Answers


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