- Home /
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
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.
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)
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. :(
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);
}
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.
}
Your answer
Follow this Question
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