Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Setting mouse cursor position

Discussion in 'Scripting' started by MarkusDavey, Jul 17, 2011.

  1. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Hello everyone!

    I'm working on an RTS camera control system, and when the user zooms in, I want it to zoom in on the mouse's position.

    Now this has a downside, and that being that when the camera moves, the focus point where the mouse was, has since moved too. Which, in my case, causes the camera to be very hard to control, and the resulting focus is often far from the initial object.

    So my solution is to set it up so that when zooming, the mouse is moved to where the world point is now, and so that the next zoom in, goes to that point again, to give the impression that the user is zooming in to where the mouse is, but it in in fact zooming in on a stored position.

    I would be using
    PHP:
    Camera.WorldToViewportPoint()
    , which was half the battle to find.

    Hopefully, what I've found on google is not required anymore. And that is, using a windows only DLL, which would suck for cross platforming.
     
  2. oneXwork

    oneXwork

    Joined:
    Mar 3, 2011
    Posts:
    266
    Camera.main.ScreenToWorldPoint(Input.mousePosition);
     
  3. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Sorry, but that's the reverse of what I want :p

    I want to set the mouse cursor to a world positon. not get the mouse's world position :p
     
  4. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    well the reverse of that is

    camera.WorldToScreenPoint (position : Vector3);

    !

    The idea of zooming the camera in and aiming it towards the point the cursor is over sounds like a neat effect. That would be different from a sniper zoom effect that just zooms in the direction the camera is pointing.
    What you describe is more like zooming in 2D, like with google maps - where the zoom is centered out from the cursor position.

    But forcing the cursor to change position does not sound like a good solution at all.

    I would suggest trying something where the camera will smoothly rotate to aim at whatever the cursor is positioned over _while_ the zooming is being done.

    P.S. - or is what you are saying is that you want the cursor to move towards the center of the screen at the same speed that the zoom centers the camera on the intial point? hmm.
    Maybe lock in on the position to center on when the zoom is initiated, but do not update the selected position during the zoom, unless the user stops zooming and starts again. This is also the way it works with google maps, etc., so maybe they are cheating too.
     
    Last edited: Jul 17, 2011
  5. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Well, I'm total crap at camera work. Currently, the camera is very jerky, the scrolling is not smoothed at all, and it's all a bit glitchy at the moment. So getting the camera to aim at what I'm wanting to get focus on is a little hard for me, because I'm crap at vector math etc.
     
  6. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    Well, this may be a little sloppy but I tested it and it works. Put it on the camera and click on objects in the scene and it will point at them.
    I am not saying this is the solution, but maybe it is the starter.
    To Do: Don't use clicking, but instead zooming initiates the raycast followed by lerping the rotation while it zooms at the same rate. Other bad stuff to fix may happen.

    In glorious javascript:

    Code (csharp):
    1. var speed : float = 1.0;
    2. private var destRotation : Quaternion;
    3. function Update() {
    4.     if (Input.GetButtonDown("Fire1"))
    5.     {
    6.         var hit : RaycastHit;
    7.         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    8.         if (Physics.Raycast (ray, hit)) {
    9.             var relativePos = hit.point - transform.position;
    10.             destRotation = Quaternion.LookRotation(relativePos);
    11.         }
    12.     }
    13.     transform.rotation = Quaternion.Lerp (transform.rotation, destRotation, Time.deltaTime * speed);
    14.  
    15. }
     
  7. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Hmm. Okay. This script is interesting :p But my issue I suppose now, is that I'm changing the "zoom" (more like panning but hey) by simply decreasing the Y value of the camera's transform. I can't figure out a better way.

    Any ideas?
     
  8. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    zoom by decreasing Y?
    Are you doing a top-down view of the playing field?

    I was anticipating that you were truly going to zoom - using camera.fieldOfView to zoom in and out, which would look like a telephoto zoom.

    Moving the camera forward/back on its local Z axis is a "dolly"shot, left/right on X is a "tracking" shot, up/down on Z (but pointing forward) is a "crane" shot. Rotating the camera on its Y axis is "panning".

    You could dolly in close to the focus point using my script example by also moving the camera forward on its Z axis.

    I guess you need to modify all of it to fit with what your arrangement is.
     
  9. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    aha! that's it! I forgot that you can just move it based off it's rotation. silly me :|

    Also, this camera controller makes me sea sick when I make it follow the mouse! I guess I need to tinker more :|

    edit:

    How to limit it so it only pitches on the X axis? all this swinging about is making me regret that energy drink :|
     
    Last edited: Jul 17, 2011