How do I keep the crosshair within a certain distance

Get help using Construct 2

Post » Wed Feb 24, 2016 8:00 am

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!
Posts: 30
Reputation: 310

Post » Wed Feb 24, 2016 8:40 am

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

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:

Code: Select all
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

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.
Mistakes were made.
Posts: 1,581
Reputation: 60,458

Post » Wed Feb 24, 2016 9:39 am

Thanks again for the help oosyrag! You're awesome! That appears to be just what I needed, as usual.
Posts: 30
Reputation: 310

Return to How do I....?

Who is online

Users browsing this forum: plinkie, tarek2, zenox98 and 16 guests