Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Object inspection camera?

Discussion in 'Editor & General Support' started by islanddreamer, Aug 16, 2006.

  1. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    Is there a pre-made object inspection camera script available anywhere? I'm currently using the Mouse Orbit control, but I'd like to be able to move the camera in and out while orbiting a static object.
     
  2. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Here's what I did for that (not a lens zoom but an actual dolly in-out while orbiting).

    I'm assuming you have the camera parented to a GO that is rotating. The way I zoomed in and out is simply to scale that GO.

    This script allows both mouse wheel and chosen keys to serve this function:

    Code (csharp):
    1.  
    2. var camparent : Transform; //camera's parent that rotates to aim camera - drag and drop in inspector
    3.  
    4. function Update ()
    5. {
    6.     var zoominput = Input.GetAxis("Mouse ScrollWheel") + Input.GetAxis("Camera Zoom Keys")/8; // /8 makes keys similar to mouse wheel speed
    7.     if (zoominput != 0)
    8.     {
    9.         camparent.transform.localScale = Vector3.Max( Vector3(.1,.1,.1), Vector3.Min( Vector3(10,10,10), camparent.transform.localScale - Vector3.one*zoominput*camparent.transform.localScale.x*.5 ) ); // .1 and 10 are near and far limits, .5 is a speed adjustment that affects both keys and wheel alike
    10.     }
    11. }
    12.  
    (EDIT: I've used "camparent" to refer to the GO being scaled, so it doesn't matter WHERE you use this script. But you could modify it to attach directly to the cam parent GO.)

    Hopefully that's a useful start.
     
  3. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    That's a cool idea (scaling the GO). I was using the mouse orbit control which doesn't have a fixed rotation, but this gives me a good head start. I'll let you know where I end up.
     
  4. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    I haven't played with the built-in mouse-orbit to know how they'd interact, I'm afraid. Good luck :)

    I just added a //comment to explain the .5, BTW.
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It's a lot easier if you just modify the existing mouse orbit script.
    It already has a variable called distance. In the script you simply have to add to that value using:

    eg.
    Code (csharp):
    1.  
    2. distance += Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * speed;
    3.  
     
  6. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    Thanks Joachim.

    This bit of code typifies for me why I'll never be a programmer. I can "read" the beginning part of the code perfectly, but then I get to "Time.deltaTime" and speed. What have either got to do with distance?

    Rhetorical question from frustrated wannabe developer, no need to answer.
     
  7. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    We've all been there, that's what the documentation is for.

    http://unity3d.com/Documentation/ScriptReference/Time.deltaTime.html

    There is no reason anyone should implicitly know what all of Unity's classes and functions do. You've just gotta look them up when you are stumped.

    If the docs don't make sense (very possible... we've all been there too :) ) then we have a common ground to help.

    Cheers,
    -Jon
     
  8. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    Yeah but how would you even know to look for Time.deltaTime if you don't know you need it?
     
  9. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
  10. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    I'm so lame!

    I can't figure out where to insert Joachim's code into the Mouse Orbit script. If I replace out line 3, I get an error about Unexpected token "+=".

    I tried inserting it into the LateUpdate function, but that didn't work either. It complains about unknown identifier "speed."
     
  11. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    you also have to add the speed variable (outside of any functions)

    var speed = 5.0;
     
  12. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    That did it. But the move in and out is really slow. I tried adjusting the speed variable, but it doesn't do much.
     
  13. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Did you adjust the speed variable in the inspector? Did you make sure that you multiply by speed when adding to the distance?

    Variables that are exposed to the inspector override the value in the script, that is the script value acts as a default value, the value in the inspector is the actual value.
     
  14. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    Yes, I tried setting the inspector speed value at 1 and 100 and the result is pretty much the same.
     
  15. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    I added Joachim's code above the
    Code (csharp):
    1.  
    2. var rotation = Quaternion.EulerAngles(y * Mathf.Deg2Rad, x * Mathf.Deg2Rad, 0);
    3.  
    line in the LateUpdate() function and it worked as advertised. With the speed set to 10.0 it was easy to tell. Is the "Time Scale" variable in Edit -> Project Settings -> Time still = "1"? I assume the mouse orbit part is still working.
     
  16. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    That did it. I set the speed to 250 in the inspector and it's now zooming in and out at a rate that works for me.

    I had previously placed Joachim's line at the end of the function. Does the order of the commands within a function influence their effect?
     

    Attached Files:

  17. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    Cool script. I'll have to play with it some, is the object you rotate around supost to be able to move freely around? Or is it ment to work more like if you frame something in Maya or Unity? Jeff
     
  18. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    Just attach this script to your camera instead of mouse orbit. Set the speed to a pretty high number (>100) and target the object you want to inspect.

    If you've got a mouse with a scroll wheel, that's all it takes.

    I think this script would work with both a moving and a stationary object, but I've only tested it with a static object.
     
  19. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    In general, the order does matter, especially with the setting the value of variables. Make sure you set them to something before you actually use them.
     
  20. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    In short, yes.

    Here's a little example of order being important:
    Code (csharp):
    1.  
    2. var i=3;
    3. i=i*2;
    4. i=i+1;
    5. print(i); // prints 7
    6.  
    Vs:
    Code (csharp):
    1.  
    2. var i=3;
    3. i=i+1;
    4. i=i*2;
    5. print(i); // prints 8
    6.  
    or even:
    Code (csharp):
    1.  
    2. var i=3;
    3. print(i); // prints 3
    4. i=i+1;
    5. i=i*2;
    6.  
    and finally:
    Code (csharp):
    1.  
    2. print(i); // compiler error.
    3. var i=3;
    4. i=i+1;
    5. i=i*2;
    6.  
     
  21. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    I'd forgotten that I had come up with this simple object inspection script several years ago (with the help of the gurus on the board, obviously -- it's depressing how little progress I've made since then!). :oops:

    Running it in 2.5, I get the following error: "WARNING: 'UnityEngine.Quaternion.EulerAngles(float, float, float)' is obsolete. Use Quaternion.Euler instead. This function was deprecated because it uses radians instad of degrees"

    The script still works, but I'd love to see it brought up to date. Any volunteers?
     
  22. islanddreamer

    islanddreamer

    Joined:
    Apr 29, 2006
    Posts:
    473
    That was easier than I thought. I looked up the new line from the camera orbit script. Here's the updated script.
     

    Attached Files: