- Home /
Throw 3D object parabolically -> From 2D Canvas to WorldSpace
I created 2 cameras 1 for the 3D object rendering for UI and other one basicly rendering the whole scene.
My question is how can I create object and throw parabolically from UI 3D object position to world space position ?
Answer by Koyemsi · May 03, 2021 at 04:07 PM
Hi.
Here is how I think I would proceed.
First thing would be to instantiate the 3D cube you want to throw, considering the one shown in the UI is only a rendering texture.
You will calculate its initial 3D position (and maybe also its scale and rotation) in order to give the impression that it’s thrown from the UI.
For the position, use Camera.ScreenToWorldPoint(), like this :
Camera cam; // this is your 3D cam GameObject UIcube; // this is your UI element showing the cube GameObject prefabCube; // no comment
float x = UIcube.transform.position.x; float y = UIcube.transform.position.y; float z = -cam.transform.position.z; Vector3 initPoint = cam.ScreenToWorldPoint(new Vector3(x, y, z));
GameObject cube = Instantiate (prefabCube, initPoint, someRotation);
For the motion, it depends.
I hope you don’t want it based on physics because you would have to apply a very finely tuned force ; seems nearly impossible. I would use an interpolation system like iTween.
For the parabolic motion, you could define a path using 3 points : the start point (initPoint), the target point and an intermediary one ; this one would be positioned between the start and target points, but with some x offset so that the path becomes a curve.
An example :
Vector3 initPoint; // the one we just defined Vector3 targetPoint; // the end of the road ;) Vector3 interPoint = targetPoint - initPoint; interPoint.x += Random.Range(1f, 3f); // give a random offset for curvature Vector3 [ ] path = new Vector3 [ ] {initPoint, interPoint, targetPoint};
// finally, do the motion : iTween.MoveTo(cube, iTween.Hash( "path", path, "time", 2f, "easetype", iTween.EaseType.easeInOutQuad ));
I didn’t try this code, so I hope it will work and be of some help ;)
I will try your concept as soon as possible and make a comeback about it.
Thank you so much in advance :)
Forgot to say that you probably will have to also tween the rotation in the same time as you move the cube, so that it imbricates perfectly with the other cubes. Use iTween.RotateTo() with the same duration.
Hint : to see all of the available iTween parameters, right-click on the "RotateTo" then choose "Reach declaration" (Ctrl+D) ; this will display the method in iTween.cs with all params.