• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Don't understand how clamp works with direction

P

Paul Simpson

Guest
Hi,

Just a quick question. I'm trying to make a top down shooter in which the player can only shoot in the direction he is facing. I can clamp the gun objects direction to the players angle no problem, but how do I clamp towards the right 180 degrees of the screen? if the player is looking towards the right (angle 0 degrees), the gun should rotate between 270 degrees (down) and 90 degrees (up) but the following doesn't work

Code:
if facing = right
{
    image_angle = clamp(point_direction(x, y, mouse_x, mouse_y), 270, 90);
}
Putting the clamp the other way around, 90 and 270, results in the gun object being clamped towards the left, which is what I want when the player object is facing left, but not when facing right.

What numbers should I put in? I tried having the clamp to be 270 and 450, which also doesn't work. I also tried two clamps depending on where the mouse is, one from 270 to 360, the other from 0 to 90, but I couldn't get that to work either.

Please help!

edit: I just tried having the clamp be between -90 and 90 to see if that would work, but no luck.
 

FrostyCat

Redemption Seeker
The two-clamp version is fine theoretically, it's just that your ranges aren't comprehensive enough. Here is a variation:
Code:
if (facing == right) {
  var mouse_dir = point_direction(x, y, mouse_x, mouse_y);
  if (mouse_dir < 180) {
    image_angle = clamp(mouse_dir, 0, 90);
  } else {
    image_angle = clamp(mouse_dir, 270, 360);
  }
}
 

Yal

šŸ§ *penguin noises*
GMC Elder
The built-in direction variable cannot be outside the range [0, 360) and will have angles automatically clamped to this range if they're outside. Values outside that range will be parsed as valid directions for all trigonometric (& lengthdir, angle_difference, etc) functions, though. This can cause a bit of confusion if you aren't aware of it.
 
P

Paul Simpson

Guest
I just tried that, I couldn't get it to work. I copied your code as you provided it, the else statement doesnt activate
 
P

Paul Simpson

Guest
Ok that does seem a bit confusing. So how can I do it? Should I try using a different function?
Does the clamping of angles to between 0 and 360 also apply to image_angle?
 
Top