Scroll to mousecursor (script)

  • #1, by CrossWednesday, 27. February 2019, 16:16 A year ago
    Hi community!

    I'm working on a wing commander type of game, that the player is supposed to control with the mouse. The background is much larger than the cockpit on screen, so the scene should always scroll towards the mousecursor. Since there is no "center on mousecursor"-action, i thought, i would write a small script for the main (invisible) character to always follow the cursor and then have the screen always centered on him. Here's the script:

    local curPos = getCursorPos() 
    local char, pos 
    function setCharPos() 
    char = game:getLink(VGameCurrentCharacter) 
    pos = char:getPoint(VCharacterPosition) 
    game.CurrentCharacter.Destination = {x = curPos.x, y = curPos.y} 
    end 
    setCharPos()

    This works fine, until the cursor reaches the edge of the screen and the scrolling sets in, because when the screen starts to move the character is "left behind" and can't "catch up" to the cursor anymore roll  Basically, the character doesn't follow the cursor anymore while scrolling. 

    Any ideas on how to improve the script or how to center the scrolling on the mousecursor itself? 

    Newbie

    92 Posts


  • #2, by afrlmeWednesday, 27. February 2019, 19:34 A year ago
    game.ScrollPosition = { x = ((game.ScrollPosition.x + getCursorPos().x) - game.WindowResolutuon.x / 2),  y = ((game.ScrollPosition.y + getCursorPos().y) - game.WindowResolutuon.y / 2) }
    1

    quick note: math is not my strong suit. I think it's correct? current scroll position + cursor position (in current viewport) - half of game resolution height/width to center the scroll position on the cursor.

    You might want to lock the mouse cursor to a specific radius or rect/bounding box to prevent it from going too far past the center of the viewport...

    Imperator

    6773 Posts

  • #3, by CrossWednesday, 27. February 2019, 20:18 A year ago
    I'm afraid, i don't quite understand. 
    I created a small "deadzone" in the middle of the screen, by setting the horizontal and vertical ScrollDistance. In this zone the character follows the cursor smoothly. But as soon as the scrolling starts, the character doesn't move anymore and gets seperated from the cursor. 
    I don't see how locking the mouse cursor to a specific radius could solve this? 

    Newbie

    92 Posts

  • #4, by afrlmeWednesday, 27. February 2019, 22:08 A year ago
    Hmm, you asked if it was possible to use the mouse cursor position instead of a character. My script example is in regards to that. The other bit about radius/bounding box was me, just musing out loud (or with text rather).

    Anyway, you mentioned a wing commander type game/mini-game. You could hide the mouse cursor by using a transparent image, & instead use an interface which contains the crosshair thing in the center of it. Now in regards to the radius thing, I was referring to locking the crosshair to a specific section of the scene, so that it follows the cursor to a degree, & you can use the offset from the center to pan the scene.

    Imperator

    6773 Posts

  • #5, by CrossWednesday, 27. February 2019, 23:24 A year ago
    Thanks for the help, Lee! 
    How you describe it, is pretty much what i had in mind. I got it working for gamepad controls, where you can move the main character directly and the background centers on him. 
    Unfortunately mouse controls won't work so far. The scrollPosition-command you provided creates more of an "teleport"-effect then a smooth movement of the background. I also tried scrollToPoint but with no luck so far.  

    Newbie

    92 Posts

  • #6, by afrlmeWednesday, 27. February 2019, 23:45 A year ago
    Aye sorry. I wasn't sure if the math was correct.

    Imperator

    6773 Posts

Write post