How do I set a projectile to fly towards a character

Just started using Construct 2? Post your questions here

Post » Thu Aug 27, 2015 2:08 am

I want a solid object to fly from off the screen, and towards and towards a character or point in the map, with a randomized +/- angle to it. To be more specific, I want multiple objects flying towards a ship, with a slight varying degree of if it will actually hit the ship or not
B
9
S
1
Posts: 49
Reputation: 551

Post » Thu Aug 27, 2015 2:22 am

Already answered the first part of my question, what I don't have now is how to randomize that angle within a certain threshhold
B
9
S
1
Posts: 49
Reputation: 551

Post » Thu Aug 27, 2015 2:47 am

did you use bullet behavior? you can set their angle of motions with random(360)
"If you want to move a mountain tomorrow, you should start by lifting stones today."
B
64
S
14
G
3
Posts: 292
Reputation: 7,188

Post » Thu Aug 27, 2015 12:42 pm

Assuming you set the angle using angle(x1,y1,x2,y2), you could change that to

>Set angle to angle(x1,y1,x2,y2) + n - random(2n)

where 2n is the variance you want. For example, if you want a 10 degree variance, that expression there would read

+ 5 - random(10)

Basically, the plus sets your upper limit, then the minus drops it to somewhere between your upper and lower.

You could also use

>Set angle to angle(x1,y1,x2,y2) + (choose(-1,1)(random(n))

which for some reason pleases me greatly, and means you only have to adjust one number. Again, n would be half of your variance. Mostly I posted the first one first to explain why the thing is happening.


(To other math nerds, sorry, I notated it that way for text simplicity.)
B
28
S
5
G
1
Posts: 62
Reputation: 2,392

Post » Thu Aug 27, 2015 2:39 pm

That makes sense thank you!
B
9
S
1
Posts: 49
Reputation: 551

Post » Fri Aug 28, 2015 12:39 am

Maybe I'm a little more new than I thought I was, so what I previously was doing to set my angle of motion was going to the event sheet, and adding the action for my object for "Set angle toward position x=480, y=270"

So, to use your code instead, where exactly where I put it, for those coordinates, and im going to have N = 5, so it's a total variance of 10
B
9
S
1
Posts: 49
Reputation: 551

Post » Fri Aug 28, 2015 1:01 pm

Oh! The thing I was talking about was for when you're not shooting towards a fixed position. In this case, the easiest way I can see in the actions would be to add an action immediately after that says

>Rotate clockwise > Degrees: (choose(-1,1)(random(5)))

which will rotate it one way (positive, clockwise) or the other (negative, counter- or anti-clockwise) 5 degrees.

The first method I posted involves choosing the angle based on two objects, typically the shooter and the target. x1, y1 is the position of the shooter, and x2, y2 is the position of the target. You can also do it sort of like you did, setting angle towards Target.X, Target.Y.

Well, have fun!
Last edited by Mike Mead on Sat Aug 29, 2015 10:16 am, edited 1 time in total.
B
28
S
5
G
1
Posts: 62
Reputation: 2,392

Post » Fri Aug 28, 2015 7:32 pm

Whenever I try to use your code, it says syntax error and highlights the parenthesis
B
9
S
1
Posts: 49
Reputation: 551

Post » Fri Aug 28, 2015 7:54 pm

Mike, I just used your advice but instead did just random (-10, 10) and it seems to have the desired result, curious what the difference would be between that, and the combination of you using choose
B
9
S
1
Posts: 49
Reputation: 551

Post » Sat Aug 29, 2015 10:18 am

Oh snap, I forgot one on the end. Updated.

Honestly, mine is only the way it is because I try to super-minimize the amount of numbers I'd need to change if I wanted to change the line (in this case, just the 5). Functionally, the only real difference is that yours technically has a range of 20, where mine has a range of 10 (between upper and lower limits). Good work!
B
28
S
5
G
1
Posts: 62
Reputation: 2,392


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests