Positioning a Camera

I am new to unity and would like to position a camera in a certain orientation with respect to a game object. Once positioned I don’t want the camera to move so it can’t be child of the game object. The way I see to try to do this seems cumbersome:

  1. Make an empty object child of the game object.
  2. Instantiate a copy of the empty object in the desired orientation with respect to the parent object. This is done in parent object model space.
  3. Use transform position to get the location of the instantiated object in world space.
  4. Position the camera in the location derived in 3.
  5. Use lookat function to point camera at the game object.

Any ideas of this would work? Is there something less convoluted I could use to accomplish the same result?

If I understand you correctly, steps 1-4 could be replaced with a call to transform.TransformPoint (which basically coverts a point local to an object into world coordinates).

Step 5 will still be needed.

you want to rotate your camera as it looks at your game object? so use lookat function

I am very new to unity and am having trouble understanding which coordinate system is currently the “local” system. I have an object Edge540 and I want to do the transform.TransformPoint(20,0,0) to create a point 20 units to the right of Edge540 in the local Edge 540 space. I am in a script accessed through a button push. How do I insure that the “current” coordinate system that the transform.TransformPoint(20,0,0) works on is the local coordinate system of the Edge540 object. I know this seems trivial but don’t know how to do it.

Thanks for any help

gameobject is Edge540.
camera.transform.position=gameobject.transform.TransformPoint(new Vector3(20f, 0f, 0f));
TransformPoint converts local space to world space. So it converts (20,0,0) with Edge540 coordinate to world space and put your camera there.

That’s all correct, except you don’t need the “gameObject” part.

Every script in Unity is attached to some GameObject, that has a Transform. The inherited properties “gameObject” and “transform” refer to exactly those. The “current transform” means nothing other than “the Transform of the object this script is on,” so “transform” is how you refer to it.

yes we know so simple things :confused: I wrote gameobject to specify “gameobject is Edge540.” to understand better :slight_smile:
this.gameobject.transform,gameobject.transform,transform.TransformPoint
Transform _transform=transform; _transform.TransformPoint