3d model orientation

I have a camera, which can rotate and move in the world freely, in every direction. I have a 3D object, and when I push a button, I want that object to “fly” (animate) in front of my camera, in good orientation, no matter in what position my camera is. See the picture: I want this instrument to animate to the camera, and I want to see it always in that position relative to the camera. (Of course, once it is in the good position I can parent it to the camera.) I also want to scale the model, that it can fit in the viewport of the camera. How can I do this?

5137-hangszer.png

I edited my code as Berenger suggested, but I am doing something wrong, please someone review my code, maybe you can spot the mistakes.

Thx for the help, I am trying to accomplish, waht you described, but it’s not seems to work yet. Please look at my code, maybe you can spot the mistakes. I was also using this pic:

5165-6a0120a85dcdae970b0120a86d9495970b.png

//half of the collider height
height=gameObject.collider.bounds.size.y/2;

//camera horizontal field of view		
h_fov= Camera.main.fieldOfView;

//screen ratio    		
rat= (float)Screen.height/ (float)Screen.width;

//according to the linked picture: tan(vertical FOV/2)    		
tan_v_fov_per_two=rat*Mathf.Tan((h_fov/2)*(Mathf.PI/180));
    		
//distance from camera
distance = height /  tan_v_fov_per_two;   		
    		
//animate in front of the camera (my cam position: x=0,y=200, z=0)
iTween.MoveTo(gameObject, new Vector3(Camera.main.transform.position.x,Camera.main.transform.position.y-distance, Camera.main.transform.position.z),2);

You can use transform.LookAt to make sure the orientation is correct, with an offset depending on your mesh orientation.

For the position, Vector3.SmoothDamp or Vector3.Lerp( …, Time.deltaTime * speed ) should do the trick.

Finally, to make sure the object fits in the viewport, you can use it’s bounding box (from the renderer or the collider), whilch gives you the opposite side of the triangle made by the camera, the object’s extremity (height) and the object’s center. You know the camera’s angle and the opposite side, so a little trigo :

// H : hypotenuse (cam -> extremity). not used here
// h : half  of the object's height (extremity -> center)
// d : distance of the object from the cam (that's what you want)
// alpha (half the cam's fov)

Tan(alpha) = h / d // SOHCAHTOA, never forget !
Tan(alpha) / h = 1 / d
d = h / Tan(alpha)

Tadaaa

I did not tested it !