- Home /
 
How can I select, single out and zoom into my object?

Suppose that I have the three cubes shown in the picture. I want to be able to select the cube on the right, then have the other two objects disappear and have the object (blue cube) centered and zoomed in on the screen.
I am wondering how can I do that?
I am thinking perhaps, when the select the blue cube, then I call destroy on the other two objects but how does the destroy function know to destroy everything but the blue cube?
Also, How do I center and zoom into my blue cube after selecting the blue cube? I think it has something to do with the camera but how do I center the view with the camera?
Answer by robertbu · Dec 18, 2013 at 05:39 AM
Here is a script to get you started. You need to add the tag "Block" in the tag manager and apply that tag to each of the blocks. Add the script to one object in the scene (not a block) like an empty game object or the camera:
 #pragma strict
 
 var camPos : Vector3; 
 var camTr  : Transform;
 var speed = 2.5;
 
 function Start() {
     camTr  = Camera.main.transform;
     camPos = camTr.position;
 }
 
 function Update() {
 
     if (Input.GetMouseButtonDown(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 += 5.0; // Zoom
                 }
                 else {
                     go.SetActive(false);
                 
                 }
             }
         }
     }
     
     camTr.position = Vector3.Lerp(camTr.position, camPos, Time.deltaTime * speed);
 }
 
               It first raycasts on a mouse down and check to see if the object clicked on has the tag 'Block'. If so, then it gets all the blocks in the scene and cycles through them. If the block is the one clicked on, then it's position is used for the move and zoom of the camera. All others are deactivated.
I tried to add a new tag but I wasn't sure how to add a new tag since it didn't let me customize the name of a tag. Should I just select player and find a player tag. Does the tag name matter?
Also, I would like this selection and camera zoom to apply to each object when selected. So if the red block is selected, the other two objects disappear. I suppose it would work for each block if I added a nested if statement?
To add a tag, select any game object, click on "Untagged" just right of the 'Tag' label in the upper left of the Inspector window, and choose 'Add Tag...' from the bottom of the list. $$anonymous$$ore info on the tag manager:
http://docs.unity3d.com/Documentation/Components/Tags.html
http://docs.unity3d.com/Documentation/Components/class-Tag$$anonymous$$anager.html
As for the code, I believe it does exactly what you want as is. Once you get tags figured out, tests it in a simple scene with just three blocks.
Hi Robertbu. Actually I looked at the first link and was thoroughly confused but the second link helped me a lot. I was confused because I didn't know I could edit the fields for size and elements. I gave the elements names and I assigned each cube a tag. It works well.
Here are some thoughts on what I want to experiment with though:
I meant to say for instance in your code, I named my blue cube, "Block1" and when I clicked on each block, the camera only zoomed to block1. However, I would like this zoom to apply to each block. So any time I select any of the blocks, the camera would zoom. So if I selected Block2 or Block 3, the camera would still zoom into them. So, I think I Just need to add more scripts for Block 2 and Block 3? $$anonymous$$aybe a switch statement?
Another thing I noticed however is when the camera zoomed to block1 , the blue cube, I could still see a bit of the white cube. I want all the cubes to disappear or be invisible from the scene while I select one of the cubes. So, to do that, would I just destroy those cubes when I select that block 1, for instance?
I think that's all I had for questions. Thank you so much again. Just like to experiment. Still learning how to use and script for Unity.
So, what I did is create a separate if statements for the red and white block, so if I select those blocks, it zooms into those blocks!, However, now I have three large if statements, I was wondering if I had several cubes e.g. 100 cubes and it would be tedious to write 100 if statements, how do I reduce the lines of code for each cube?
So, now I just need to work on destroying the other cubes from the scene and reducing the code.
Note, the code assumes that the tag is the same for all the blocks. That is, tag all the blocks with a "Block" tag, then run the script. If you need to encode the color, then you can have it in a variable attached to a script on the block, or you can encode it in the name. If you really need the tags to be different, then there are other ways to solve this problem, but I'd have to understand more about your game to recommend a specific change.
Answer by Kiloblargh · Dec 18, 2013 at 12:51 AM
Don't use Destroy, use thisCube.gameObject.SetActive (false).
You could have a Transform[3] array and loop through it and compare to the transform of the clicked cube- if each element is not the transform of the cube you just clicked on, set it inactive.
Use Vector3.SmoothDamp to move the camera from its current position to the target position if it is not already at the target position (which is the camera's y and z and the clicked cube's x).
Hi, is it possible to give me an example of what you mean?
What's your final goal; is it to be able to click any object anywhere in the scene and make all the others go away and zoom the camera in to the one you clicked?
Or is it always just going to be 3 boxes lined up the same way and that's it? If it's the latter, all you have to do is create 3 different AnimationClips, and play one of the three based on the cursor x position on click.
Your answer
 
             Follow this Question
Related Questions
instatiate gameobject after 1 second 1 Answer
If the player is behind the camera destroy it. 3 Answers
Camera doesnt follow selected character 0 Answers
Raycast Object Selection 3 Answers
Arial camera get selected point 2 Answers