- Home /
I want to select an object and have the camera zoom in but inactive the other objects. I am having problems
This is a follow up to this question: http://answers.unity3d.com/questions/598995/how-can-i-select-single-out-and-zoom-into-my-objec.html#comment-600180
For more complex objects and scenes, I tried to assign the script to my main camera. However, my objects disappeared including what was supposed to be zoomed in and rendered. If I play the scene for game view and then pause the view, it appears that the piece that was supposed to be isolated and zoomed into is shown (the only object shown out of the whole structure) but for some reason, it does not render in game view. It only shows up when I pause the view.
Why is that?
Answer by robertbu · Dec 20, 2013 at 02:49 PM
As a guess, the issue is the size of the object. The code zooms by moving the camera forward. If the object is large, then moving the camera in puts the surface of the object beyond the near clip plane of the camera. Result: the object disappears. Edit line 23:
camPos.z += 5.0; // Zoom
Select a smaller number than 5.0. Note this may not be the complete or right fix for your game. There are a couple of other things you can do. The first is in your initial setup, move the camera back away from object and then narrow the field of view of the camera. This will give you more working room for your zoom while maintaining the same object size. You can then edit the '5.0' in the line above as appropriate.
The second thing that can be done is to recode the zoom so that it change the field of view rather than moves the camera. Given the camera was already going to move sideways to center the object, moving it forward was the simplest solution.
Hi, I changed my zoom setting to 0.25 and I was able to see my object. However, my object was not zoomed in and it appeared small. It was isolated from the other objects but not zoomed in. So, I was wondering how do I have it zoomed in but appear in the scene? I was looking at changing the field of view but I wasn't sure how I would do that. Would you mean if that I need to adjust the near and far clipping plan?
The default clipping planes are fine, so unless you've edited them, changing the clip planes is not the solution. What I recommend is as follows:
In edit mode, click on the camera in your hierarchy.
In the Inspector decrease the field of view. For this test, cut it in half. For example if it is set at 60, change it to 30.
Now double the distance of your camera to your objects. For example if your camera is at -10 on the 'z' axis as is the default, move it to -20 on the 'z' axis.
Now run your app. You may have to adjust how far you move the camera forward to get the zoom you want. For example:
camPos.z += 8.0;
Note zoo$$anonymous$$g by adjusting the camera's field of view is another option. Code to zoom the FOV has been posted to UA many times.
The camera is orthographic. I would have to change the size of my orthographic camera for the field of view? (as there's no field of view for orthographic cameras). Also, I disabled camPos.x and CamPos.y because I don't need the object itself to move (It moves when I select it) and it seems fine. I just need to zoom it in the z direction but I'm not sure how to zoom in z direction without having the camera make the object disappear.
If the camera is orthographic, then you will have to change the orthographic size of the camera to zoom. Here is a bit of demonstration code on one way to zoom the size. Note the small the size, the larger the object (the more zoomed in):
#pragma strict
var speed = 2.5;
function Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z)) {
OrthoZoom(1.5);
}
}
function OrthoZoom(size : float) {
var cam = Camera.main;
while (cam.orthographicSize != size) {
cam.orthographicSize = $$anonymous$$athf.$$anonymous$$oveTowards(cam.orthographicSize, size, speed * Time.deltaTime);
yield;
}
}
Hmm, nothing happens to the zoom. Here's how I manipulated your code:
var camPos : Vector3;
var camTr : Transform;
var speed = 2.5;
function Start() {
camTr = Camera.main.transform;
camPos = camTr.position;
}
function Update() {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit) && hit.collider.tag == "block") {
var blocks = GameObject.FindGameObjectsWithTag("block");
for (var go : GameObject in blocks) {
if (go == hit.collider.gameObject) {
//camPos.x = go.transform.position.x;
// camPos.y = go.transform.position.y;
// camPos.z += .05; // Zoom
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z)) {
OrthoZoom(3);
}
}
else {
go.SetActive(false);
}
}
}
}
camTr.position = Vector3.Lerp(camTr.position, camPos, Time.deltaTime * speed);
}
function OrthoZoom(size : float) {
var cam = Camera.main;
while (cam.orthographicSize != size) {cam.orthographicSize = $$anonymous$$athf.$$anonymous$$oveTowards(cam.orthographicSize, size, speed * Time.deltaTime);
yield;
} }
Your answer
Follow this Question
Related Questions
camera zoom 1 Answer
Clamping a 2D camera while zooming. 0 Answers
Limiting Camera Movement 1 Answer
Camera scripting references 0 Answers
Camera too zoomed in in built game 1 Answer