How do I set bullet moving angle to left analog stick?

Get help using Construct 2

Post » Thu Feb 12, 2015 5:54 pm

Hi.

I'm trying to figure out how to set the moving angle of my spawned bullets to shoot in the direction the left analog is facing when I shoot. I can't get it to work properly. Does anyone here know how to achieve this simple task? :P

Thank you in advance!
B
57
S
24
G
14
Posts: 772
Reputation: 13,135

Post » Thu Feb 12, 2015 7:13 pm

This question has come up a few times on these forums lately, it seems. :)
To find the angle of the left analog stick you can use this expression.
Code: Select all
angle(0, 0, Gamepad.Axis(0, 0), Gamepad.Axis(0, 1))


Then you just need to set On created - Set Bullet angle to *the code above*

That should work for you.
Be sure to check out my Metroidvania game, A Hole in the Earth
B
59
S
24
G
3
Posts: 359
Reputation: 5,683

Post » Thu Feb 12, 2015 7:22 pm

@ryanrybot Wow, that works great. Thank you so much! What is the logic behind this sorcery? And can I lock it so it just goes in 4 or 8 directions?

I have to learn to do this stuff on my own. But couldn't figure it out.
B
57
S
24
G
14
Posts: 772
Reputation: 13,135

Post » Thu Feb 12, 2015 7:56 pm

angle(X1,X2,Y1,Y2) finds the angle "between" two points (mathematically incorrect definition, but works for us).

So you have an angle value that's between 0 and 360 - if you want to have 4 directions just divide by 90, round, then multiply by 90. For 8 directions use 45.

So... round(angle(0, 0, Gamepad.Axis(0, 0), Gamepad.Axis(0, 1))/45)*45
B
19
S
6
G
7
Posts: 1,101
Reputation: 6,146

Post » Thu Feb 12, 2015 8:16 pm

@Somebody I see, now I understand how to do all of this. Thank you!

One last question. Now my crosshair follows the analog stick. But when I stand still after running left my character is facing left but the crosshair jumps back and faces right. Do you know how to keep the crosshair at the same angle as the character when the analog stick is not in use?

issue.png
You do not have the required permissions to view the files attached to this post.
B
57
S
24
G
14
Posts: 772
Reputation: 13,135

Post » Thu Feb 12, 2015 8:22 pm

If it jumps back then it's probably due to the sticks generally being a little imprecise (either plain value shifts or a heavy thumb)...

EDIT: Oh, turns out C2 already has a deadzone check - right in the Gamepad object settings. Maybe see if yours is high enough?

I guess then you need a check for non-zero values - otherwise at 0,0 the angle will always be 0, thus facing right.

[IGNORE EVERYTHING BELOW] - games usually have a "deadzone" so you might want to put in a little check, like:

Gamepad.Axis(0, 0) is between values -ZZZ and ZZZ <== Some value here - I don't know what the value range for the gamepad is, maybe it's 0-1, then something like 0.05 might do, maybe it's a more usual 255, either way you could probably check debug to see the values.

OR

Gamepad.Axis(0, 1) is between values -ZZZ and ZZZ

DO THE ANGLE THING.
B
19
S
6
G
7
Posts: 1,101
Reputation: 6,146

Post » Thu Feb 12, 2015 10:21 pm

@Somebody Awesome, thanks. I solved it by checking if the axis of the analog stick was 0 and the character is mirrored then mirror the crosshair else not mirror :)
B
57
S
24
G
14
Posts: 772
Reputation: 13,135

Post » Thu Feb 12, 2015 10:47 pm

Hi @Anonnymitet! I'm glad that worked for you! :D
There is absolutely no harm asking. In fact I asked this very question a couple weeks ago, which is why I knew the answer. :P
Be sure to check out my Metroidvania game, A Hole in the Earth
B
59
S
24
G
3
Posts: 359
Reputation: 5,683


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests