DE EN FR ES IT CZ
Back Back to top

Scroll to mousecursor (script)

This thread is a few years old. Some details may refer to an earlier version of Visionaire Studio. Current tutorials

  • #1, by Cross 8 years ago Quote
    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? 
  • New to Visionaire Studio?

    Create point and click adventures without coding. The free version covers up to 25 scenes per project. For more scenes or to earn money with your game, you need a license.

    See the Indie Bundle Try it for free
  • #2, by afrlme 8 years ago Quote
    game.ScrollPosition = { x = ((game.ScrollPosition.x + getCursorPos().x) - game.WindowResolutuon.x / 2),  y = ((game.ScrollPosition.y + getCursorPos().y) - game.WindowResolutuon.y / 2) }

    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...
  • #3, by Cross 8 years ago Quote
    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? 
  • #4, by afrlme 8 years ago Quote
    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.
  • #5, by Cross 8 years ago Quote
    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.  
  • #6, by afrlme 8 years ago Quote
    Aye sorry. I wasn't sure if the math was correct.