How do I limit the turning angle of a turret?

0 favourites
  • 5 posts
From the Asset Store
A well commented RPG game template to learn from or use as a base for your own game!
  • I'm using the RTS Template and have a have a tank (base sprite and turret sprite) and I want to limit the rotation angle of the tank turret. Currently it spins 360 on the base, I want to limit this so that it can only spin 130 degrees to the right or left (cannot do a complete 360).

    Can anyone suggest a way of doing this (I thought it might be a behaviour of the turret object, if it is, then it isn't obvious to a newb).

  • Can you post the capx ? It much easier to help you if you attach the capx !

  • Think you can do it like this, however this is just a theory so might not work correctly

    But I think you can do it by adding two checks.

    first you need to know the angle of the chassis, as its based on its angle to determine the limits of the turret.

    You need to calculate both limits. will call these High limit and low limit. Which simply means that high limit is the clockwise limit, and the low limit the counter clockwise limit.

    To calculate the high limit.

    Chassis.angle + 130 (Turret limit)

    This number you need to check if is more than 360.

    Chassis.angle + 130 > 360

    If that's the case you know that it have passed the 360 marker and will start to go from 0 and up.

    So to calculate that one, you have to do the following,

    (Chassis.angle + 130) - 360

    That will give you the high limit, if Chassis.angle + 130 is more than 360.

    Its pretty much the same for the lower limit.

    Chassis.angle - 130 < 0

    To calculate the lower limit if it passes 0 angle marker.

    (Chassis.angle - 130) + 360

    Will give you the lower limit.

    Example using real numbers:

    Chassis is moving upwards at an angle of 245 and the turret is just following along at the same angle.

    Lets assume that an enemy shows up at angle 60 (Somewhere behind the tank)

    So to see if the turret can actually hit it, we calculate the two limits.

    High limit (Chassis.angle + 130 > 360)

    245 + 130 > 360 = 375

    Since that pass the marker we calculate the following:

    375 - 360 = 15 High limit angle

    Low limit (Chassis.angle - 130 < 0)

    245 - 130 < 0 = 115

    Since this doesn't pass the 0 angle marker we can use the calculated angle as the lower limit.

    So the tank turret will be able to shoot at everything going from Low limit angle 115 to high limit angle of 15.

    So in this case it wouldn't be able to attack the enemy.

    Anyway its just a theory, I haven't actually tested if it works, but think it will

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Joskin I'm using the RTS Template, so File > New > Template: Real Time Strategy (RTS)

    nimos100 I'll work through this and let you know how I go, thanks for taking the time to write such a detailed answer

  • jwoods

    Hello,

    Did you solved this problem?

    Can you share solution?

    Thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)