Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Eowyn27 · Dec 18, 2013 at 12:44 AM · cameradestroyselectioninstatiate

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

alt text

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?

sample.png (3.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eowyn27 · Dec 19, 2013 at 02:55 PM 0
Share

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?

avatar image robertbu · Dec 19, 2013 at 05:23 PM 0
Share

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.

avatar image Eowyn27 · Dec 19, 2013 at 05:54 PM 0
Share

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.

avatar image Eowyn27 · Dec 19, 2013 at 06:15 PM 0
Share

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.

avatar image robertbu · Dec 19, 2013 at 06:59 PM 0
Share

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.

Show more comments
avatar image
0

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eowyn27 · Dec 18, 2013 at 03:59 AM 0
Share

Hi, is it possible to give me an example of what you mean?

avatar image Kiloblargh · Dec 18, 2013 at 04:04 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges