How do I keep the crosshair within a certain distance

0 favourites
  • 3 posts
  • I have a custom sprite named crosshair and am setting it to my mouse position every tick. I'm then shooting bullets at the cursor whenever clicked. I chose this method for eventual gamepad support. But I am a bit lost when it comes to keeping the cursor 100 pixels or so from the player without going any further. What are the best ways to set it up to not conflict with controller-analog aiming?

    The closest I'm thinking about is like Nuclear Throne with it's controller support. My game's not really the same style but the crosshair is similar to what I was trying to do. Thanks!

  • Sorry I'm not familiar with Nuclear Throne, but here are some expressions that might be useful to you.

    [quote:rgz7gf0m]clamp(x, lower, upper) - Return lower if x is less than lower, upper if x is greater than upper, else return x.

    distance(x1, y1, x2, y2) - Calculate distance between to points

    In the event where you set the crosshair to your mouse position every tick, add the condition system compare two values: distance(player.x,player.y,mouse.x,mouse.y)<100. This will make it so that once your mouse gets farther than 100 pixels away from the player, the crosshair will stop updating its position. Probably not the effect you are looking for though...

    Clamping your crosshair position to player x and player y +-100 would result in a square range around your player, also probably not what you are looking for...

    Quick workaround I can think of is to use both together by using two actions every update tick - move the crosshair to player.x, player.y, and then move it at angle towards your mouse, amount being distance from player to mouse clamped at maximum 100. It looks like this:

    Every Tick | Move crosshair to player.x, player.y
               | Move crosshair clamp(distance(player.x,player.y,mouse.x,mouse.y),0,100)
                 pixels towards angle(player.x,player.y,mouse.x,mouse.y) degrees[/code:rgz7gf0m]
    
    Unfortunately I have no idea how you plan on setting up controller aiming so this might not work with that! I hope it gives you some ideas to work with though.
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks again for the help oosyrag! You're awesome! That appears to be just what I needed, as usual.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)