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 /
avatar image
0
Question by Achillies904 · Sep 08, 2015 at 09:27 AM · uicanvasmove an objectdrag-and-dropworld space

Problem moving a GameObject with an OnDrag event when the canvas is in world space

I am trying to use the OnDrag event to change a gameobjects position. I've done it before and it worked fine, but this time I can't get it to be accurate. The only difference between this project and the others is that this time the canvas is on world space coordinates.

I hava tried this code:

 public void OnDrag(PointerEventData eventData)
 {
     transform.localPosition = eventData.position;  
 }

and this one:

 public void OnDrag(PointerEventData eventData)
 {
     transform.localPosition = Camera.main.WorldToScreenPoint(eventData.position);
 }

and this one:

 public void OnDrag(PointerEventData eventData)
 {
     transform.localPosition = eventData.pointerPressRaycast.worldPosition;
 }

and I even tried working with the delta, which gave me the best outcome, but it is still not very good:

 public void OnDrag(PointerEventData eventData)
 {
     Vector3 t = new Vector3(eventData.delta.x, eventData.delta.y, 0f);

     transform.localPosition += t;        
 }

I also tried using the transform.position instead of localPosition, and allot of other combinations, Including stuff that I have never tried before like WorldToViewPort, even though I knew it was a Hail Mary.

I would really appreciate help with this, because the movement is very bad right now, and as far as I know the only thing that has changed from other projects is the world space canvas.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by afraism · Aug 14, 2018 at 06:57 AM

Try This:

 Vector2 pos;
 RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out pos);
 transform.position = myCanvas.transform.TransformPoint(pos);
 
Comment
Add comment · Show 3 · 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
avatar image ArcIo · Sep 10, 2018 at 05:51 PM 1
Share

This is the correct answer. I faced this very bug about a month ago and moved on to other challenges. I found this today and after about 2 $$anonymous$$utes. All is working as expected. Bravo @hosseinafra

avatar image Fattie · Jun 07, 2020 at 10:15 PM 0
Share

Thanks for the great tip! Sent 100 points.

avatar image afraism Fattie · Feb 02, 2021 at 12:42 PM 0
Share

Thanks @Fattie

avatar image
1

Answer by Allthebees · Aug 11, 2017 at 06:52 AM

I'm not sure I've fully understood the question and this is quite old but I think this could be of use to someone so I'll post it anyway.

If you're working in world space then you need to be dealing with things in the world space.

Input.mousePosition returns a position on screen. The current project I'm working on is top-down so it's fairly simple case of using a ray to convert the screen position to a location in the world:

     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     myObject.transform.position = new Vector3(ray.origin.x, 0, ray.origin.z);

An additional conditional check can be used by raycasting to be sure you're on the ground. (If your game is not top down you can use this raycast to find the position in world space (of the ground)):

 if (Physics.Raycast(ray, out hit))
Comment
Add comment · Show 1 · 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
avatar image QuietPenguin · Nov 18, 2020 at 09:39 PM 0
Share

I have been working on this problem for 2 days now, and this is the only thing that worked for me! I appreciate you taking the time to explain what's going on too.

Figured I'd post and say that not only did this work for me, but it was absolutely of use to someone and I thank you sincerely for taking the time to comment this.

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

8 People are following this question.

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

Related Questions

UI in World Space doesn't respond 1 Answer

Dragging UI elements in Screen Space canvas 1 Answer

How to render an image always on top? Already tried some solutions 0 Answers

OnPointerEnter blocked by something 2 Answers

EventSystem raycasting on World Canvas gameobject always returning worldPosition of zero 2 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