Unity Community


Results 1 to 9 of 9

  1. Location
    Perth, Western Australia
    Posts
    258

    C# Setting mouse cursor position

    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 Code:
    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.
    Skype: Markus.davey


  2. Location
    Singapore
    Posts
    244
    Camera.main.ScreenToWorldPoint(Input.mousePosition );

    OneXWork On FaceBook
    A demo of stage 1 in Zero Line:3d world(currently it is being called ZeroLine: Legend of the Grip)
    Zero Line:The Legend of Grip
    A Flip puzzle game on kongregate
    Flip Cube
    A simple clicking puzzle game on kongregate
    The Longest Rope
    A mobile Flip Cube version puzzle game on kongregate
    Flip Cube M Beta
    New Zero Line Adventures game on Apple App Store
    Zero Line:Adventures of Little Luka


  3. Location
    Perth, Western Australia
    Posts
    258
    Quote Originally Posted by wx91 View Post
    Camera.main.ScreenToWorldPoint(Input.mousePosition );
    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
    Skype: Markus.davey


  4. Location
    ohio
    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 by priceap; 07-17-2011 at 12:07 PM.


  5. Location
    Perth, Western Australia
    Posts
    258
    Quote Originally Posted by priceap View Post
    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.
    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.
    Skype: Markus.davey


  6. Location
    ohio
    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:  
    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.         if (Physics.Raycast (ray, hit)) {
    8.             var relativePos = hit.point - transform.position;
    9.             destRotation = Quaternion.LookRotation(relativePos);
    10.         }
    11.     }
    12.     transform.rotation = Quaternion.Lerp (transform.rotation, destRotation, Time.deltaTime * speed);
    13.  
    14. }


  7. Location
    Perth, Western Australia
    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?
    Skype: Markus.davey


  8. Location
    ohio
    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. Location
    Perth, Western Australia
    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 by MarkusDavey; 07-17-2011 at 12:54 PM.
    Skype: Markus.davey

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •