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 /
avatar image
0
Question by N8Squared · Mar 27, 2020 at 01:27 PM · uiworldspacescreentoworldpointscreenspace

World space and UI space

I am making an object in world space convert to UI space then move to another UI object like how it works when you collect a wumpa fruit in crash bandicoot, in fact EXACTLY liike how it works in crash bandicoot. I just cant seem to get it to work, i have tried spawning it in world space as a UI object -> moving it into screen space -> then moving it to the desired object but it didn't work (I probably just didn't understand it, im new to unity)

If someone can help then that would be great

[Crash basis]: http://www.youtube.com/watch?v=jfRAF_MXQNA

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Infenix · Mar 27, 2020 at 01:41 PM

If that's effectively what you're looking for, I would use the Camera.WorldToScreenPoint() function. So when you start moving your object, you make it target this point, and once you decide it is close enough to the point, then you update your UI and then destroy your gameobject.

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
avatar image
0

Answer by N8Squared · Mar 27, 2020 at 02:27 PM

Yes but i try to do that and it doesnt work? i dont know why.. and when i get close to what im looking for then it goes the wrong way (Excuse me sing ms paint)

alt text

my code is basiclly

 public GameObject Player; //Is World Space
 public GameObject UIEndPoint; //Is Screen Space
 public Camera Cam;
 public Vector3 StartPoint;
 public Vector3 EndPoint;
 public float Smoothness;
 
 void Start{
 
 StartPoint = Cam.WorldtoScreenPoint(Player.transform.position);
 EndPoint = UIEndPoint.transform.position
 
 
 }
 void update{
 
 transform.position = new Vector3 (Mathf.Lerp(StartPoint.x, EndPoint.x, Smoothness), Mathf.Lerp(StartPoint.y, EndPoint.y, Smoothness), Mathf.Lerp(StartPoint.z, EndPoint.z, Smoothness))
 
 
 }

And it still doesn't work. :(


boi.png (14.2 kB)
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 Infenix · Mar 27, 2020 at 06:42 PM 0
Share

Ok I found how to fix it, I made some mistake but now it works.

First of all, if you're using the Lerp function, know that unity has a lerp function for aproximately everything that can use it. So in the case of a Vector3, you can use the Vector3.Lerp functions that returns a complete Vector3.

I solved it by changing the world to screen to screen to world, because you want to move the object in the real world and to make it look like it's going to the UI. I do it so you depth while moving. If we used WorldToScreen we would have need to change it into UI directly and then move it.

In my ScreenToWorld, i replace the Z coordinate by 1 to not get the point at the camera position but on it's renderer, so 1 to normalize it. Any positive value looks to work.

Finally, i change the localscale too over time in order to have it reduced when it goes to the UI. Without it the object arrives directly on your camera at its real size and... that's ugly. You may change the 1- Smoothness to make it goes between 1 and value between 0 to 1 to make it more realistic maybe, like at least 0.9 to don't have the object being rediculously small at the end.

Hope it helps you, it's working for me !

I have this script:

     public GameObject Player; //Is World Space
     public GameObject UIEndPoint; //Is Screen Space
     public Camera Cam;
     public Vector3 StartPoint;
     public Vector3 EndPoint;
     [Range(0, 1)] //Range between 0 and 1 that evolves from 0 to 1 during the animation
     public float Smoothness;
 
     //Scale of the object before starting to lerp it
     Vector3 ownScale;
 
     void Start()
     {
         ownScale = transform.localScale; //Save the scale before changing
 
         ///Find the points to start and to target, will need to put it in Update()
         ///if the player is moving during the animation
         StartPoint = Player.transform.position;
         EndPoint = Cam.ScreenToWorldPoint(new Vector3(UIEndPoint.transform.position.x, UIEndPoint.transform.position.y, 1));
     }
 
     void Update()
     {
         transform.position = Vector3.Lerp(StartPoint, EndPoint, Smoothness);
         transform.localScale = ownScale * (1 - Smoothness);
     }
avatar image
0

Answer by Skuxxnosch · Mar 27, 2020 at 04:41 PM

Im not sure if they did it this way in the original, but this is how i would do it with unity. How about deleting the collectable upon collision with the player and then moving a Sprite representing the collectable from the player-screen-position in your canvas. (Looks like this in the youtube link, because the apples sometimes stay a little while in the same screen pos. 0:42 in the video)

 //set up ui with an endpoint img and an apple img somewhere in the canvas
 //hit space -> apple image will move from player screen pos to enpoint
 public GameObject Player; 
 public GameObject UIEndPoint; 
 public GameObject UIappleImg; //ui image moving across screen
 public Camera Cam;
 public float Smoothness; //lerp speed
 private Vector3 EndPoint; //UI apple counter position
 
 void Start()
     {       
         EndPoint = UIEndPoint.transform.position;
     }
 
 void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space)) //debug to show effect. Hit space several times
         {                        
             //TODO: swap if(input) to if(collision with collectable)
             //TODO: destroy collectable and spawn apple img

             //position apple img at player screen pos
             apple.transform.position = Cam.WorldToScreenPoint(Player.transform.position);
         }
         apple.transform.position = Vector3.Lerp(apple.transform.position, EndPoint, Smoothness);
         //There is a Vector3.Lerp() for lerping positions.
        // Easier than Mathf.Lerp() which takes only one float.
     }




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

209 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Fitting GOs from world point to screen space UI. 1 Answer

Unity Dropdown - can't render unity dropdown option list in world space 0 Answers

Aligning UI Elements in Code 0 Answers

UI object marker showing up when object is outside view 0 Answers

Scalewithscreensize equivalent for worldspace canvas's 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