Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by kinklade · May 17, 2015 at 07:44 AM · 2dcanvasworldspaceviewport

Move a 3d object from world space in the camera UI

I need to move a non-UI object in my scene to a the position of an object on the canvas. Here is an illustration of what I am trying to do, the worldspace object to move along the red arrow to the UI element:

alt text

I have tried lots of things and searched at length, the best I could do was as below (except itdoesn't quite work):

 void FixedUpdate()
     {
             Vector2 StartPosition = transform.position;                      
             Vector2 DestinationPosition = Camera.main.ScreenToWorldPoint(canvas_UI_element.transform.position); 
             
             
             transform.position = Vector3.MoveTowards(StartPosition, DestinationPosition, MoveSpeed);
             
         
     }


2d-move-issue.png (330.0 kB)
Comment
Add comment · Show 1
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 kinklade · May 17, 2015 at 07:25 AM 0
Share

Apologies in my example the object moves to see$$anonymous$$gly random points in world space when the camera moves. I swapped it as you suggested and again the destination point seems arbitrary, rather than smoothly on the canvas from where it started to the UI element.

Perhaps I am approaching this in the wrong way?

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by kinklade · May 17, 2015 at 04:41 PM

I have finally come up with a solution that works. Here it is for anyone else that might have trouble doing this:

     void FixedUpdate()
     {
     
             //Get the location of the UI element you want the 3d onject to move towards
             Vector3 screenPoint = ui_element_gameobject.transform.position + new Vector3(0,0,5);  //the "+ new Vector3(0,0,5)" ensures that the object is so close to the camera you dont see it
             
             //find out where this is in world space
             Vector3 worldPos = Camera.main.ScreenToWorldPoint( screenPoint );
             
             //move towards the world space position
             transform.position = Vector3.MoveTowards(transform.position, worldPos, currentMoveSpeed);
         
     }
Comment
Add comment · Show 2 · 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 kinklade · May 17, 2015 at 12:41 PM 0
Share

without looking too much into it I'm not sure why the above works better than the original code, possibly that they were vector 2s

avatar image amitsri20 · Mar 19, 2020 at 05:51 PM 0
Share

Works perfectly!

avatar image
0

Answer by anilo · Jul 26, 2015 at 09:22 PM

Im struggling with the same thing. Were you able to figure out a solution for this?

The solution you have outlined simply moves my cube to another position, but not exactly to the UI element.

Comment
Add comment · Show 2 · 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 kinklade · Jul 26, 2015 at 09:30 PM 0
Share

Yep the pasted is what im using.

quite tricky for me to understand what your issue is from the description alone :/ you have code snippets or any more info?

avatar image anilo · Jul 30, 2015 at 03:08 AM 0
Share

heres info about everything that I've tried.... http://answers.unity3d.com/questions/1015433/move-3d-object-to-2d-canvas.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Move an object in world space on the viewport 0 Answers

World Space not displaying my UI. Any suggestions? 2 Answers

Screen Space Overlay Canvas blocking World Space Canvas 1 Answer

Strange UI Button hover offset when using Worldspace Canvas in VR 4 Answers

how to move an UI image just in the surface of the canvas??? 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