Trying to place a camera perpenicular to a midpoint

…and failing miserably.

I have two GameObjects in my scene that I am trying to frame in a camera. I have the GameObjects stored as pc and npc. I am doing the following to get the midpoint between them:

transform.position = Vector3.Lerp(pc.position, npc.position, 0.5);

which works fine, the object holding my camera ends up between them. However, from this point on I am brickwalling (due in no small part to my limited understanding of vectors). What I want to do is move the camera object perpendicular to the line between the players, so that it’s off to the side of them a bit. However, when I try:

dcam.transform.localPosition += new Vector3(5,0,0);

with dcam being my actual camera, it’s still moving the camera along the world axes and not along the local axes.

What am I doing wrong here?

localPosition moves the camera along its location relative to its parent. It’ll always move along world space when translated whether it’s in position or localPosition.

What YOU want is transform.right, transform.forward, or transform.up. Those vectors give the camera’s position relative to itself.

For instance, if you wanted to move it three spaces down relative to itself, you’d do this: transform.Translate(-transform.up * 3);

Well, you are not moving the camera perpendicular to the line between the players. :slight_smile:

First you must find that perpendicular direction:

perpDir = Vector3.Cross(pc.position-npc.position, Vector3.up).normalized;

(This is a direction perpendicular to both the line between the players and to the up direction.)

Then you can translate your camera in that direction and you may also want to make it look in the opposite of that direction.

Thanks for the help guys, it’s working now. :slight_smile: