0 Favourites

How do I set a projectile to fly towards a character

  • 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

  • Already answered the first part of my question, what I don't have now is how to randomize that angle within a certain threshhold

  • did you use bullet behavior? you can set their angle of motions with random(360)

  • 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.)

  • That makes sense thank you!

  • 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

  • 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!

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Whenever I try to use your code, it says syntax error and highlights the parenthesis

  • 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

  • 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!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
99 10,137
karshinkoff's avatar
karshinkoff
Unread hot topic
88 9,240
goldentreee's avatar
goldentreee
Unread hot topic
56 6,150
Tom's avatar
Tom Construct Team Founder