Moving Camera to a Selected object

Hi,

so I am making an RTS type of game and I already have scripts that lets me move, rotate, pan, and zoom the camera the way i want to. I also have scripts that let me place objects and select so i can do stuff with them. Now the one thing i would need help with is that when i select the object press a button that pops up (which i have working) that the camera would move to a position close to the object and stay there in a fixed position then return back to normal moving camera (like pressing escape or something to get back to normal moving camera). I basically know what i need to do like store the cameras position in a variable as well as the objects position (probably using transform) then calculating the offset between the two points and using vector3.lerp to smoothly move it to that position.

I’ve looked around the web quite a bit to figure out how i would put it into code form but didnt have any luck yet. I don’t need anyone to give me the entire code but more like the general structure like what functions to use or even just describing how i should do it would be more than enough. Thanks in advance.

Try using this

from unity manual

Moving the Camera Along a Ray
It is sometimes useful to get a ray corresponding to a screen position and then move the camera along that ray. For example, you may want to allow the user to select an object with the mouse and then zoom in on it while keeping it “pinned” to the same screen position under the mouse (this might be useful when the camera is looking at a tactical map, say). The code to do this is fairly straightforward:-

var zooming: boolean;
var zoomSpeed: float;

if (zooming) {
	var ray: Ray = camera.ScreenPointToRay(Input.mousePosition);
	zoomDistance = zoomSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
	camera.transform.Translate(ray.direction * zoomDistance, Space.World);
}

Touch for mobile (Android/ios)
check this link: moving camera with touch screen unity - Code Examples & Solutions

credit for the person