How to constrain cannon angle to the player angle, so it rotates to shoot the player.

famekraftsfamekrafts Member, BASIC Posts: 834
I am creating a boss enemy which will shoot the player, it will upto rotate to a particular angle and shoot the player in real time. So have to contsrain it's rotation to a particular angle as well as make it rotate by ai according to player x and y position.

How do I achieve this effect? Can any one help?

Comments

  • ashtmjashtmj Member, PRO Posts: 405
    constrain your boss' rotation to

    vectorToAngle( self.Position.X - game.PLAYER X , self.Position.Y - game.PLAYER Y )

    you'll also need to make 2 global attributes called player X and Y and constrain those to your player's self position X and Y
  • famekraftsfamekrafts Member, BASIC Posts: 834
    I already have player x and y position. I didn't understood I have to subtract the two value. Testing now.
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Thanks it worked, now how do I constrain the cannon angle say not more tha 70 degrees, otherwise the cannon will look broken with the shooting barrel breaking apart from the body if the angle is more than that?
  • ashtmjashtmj Member, PRO Posts: 405
    Never had to do that... I suppose you could make 2 new angle attributes within your cannon actor called MAX and MIN lets say your cannon starts at a rotation of 270 you could set the min to 220 and the max 320, and make the condition of the constrain rotation rule that the cannon's self rotation is above the self min and below self max.

    that will keep it from going beyond, but you'll must likely need to make 2 more rules to bring it back,
    if self rotation is equal or greater then self max(320), change self rotation to 319
    if self rotation is equal or less then self min(220), change self rotation to 221

    I THINK that would work...but I haven't tested it so maybe not
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Not working :) my player can fly so it can go up. When I am trying this my cannon gets flipped and the problem is it stops moving in real time and keeps constrained at that angle till I move my player beyond the max as specified.
  • famekraftsfamekrafts Member, BASIC Posts: 834
    If I can change the anchor point of my actor both xy to right I might be able to avoid this constraining all together but the anchor points are disabled.
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Searched and got this, I think this http://forums.gamesalad.com/discussion/49327 really helps. Saved me time.
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Ok having another problem now. The cannon will shoot a long lightning every 5 seconds, the problem is when I give it same rotation code as used for the cannon its appearing in the direction opposite to player. How do I make it rotate to intersect the player?
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Also tried creating an attribute called cannon rotate. Put the self rotation of cannon in this attribute and made the lightning rotate equal to cannon rotate. But it's still not working.
  • ashtmjashtmj Member, PRO Posts: 405
    @wickedsunny If it is just firing the reverse of the angle you need tell the cannon to spawn it with a rotation of 180 relative to self then it will be where you need it.

    I would think you can just tell the cannon to spawn the lightning at rotation relative to self and it would work tho, then you don't need to have extra code within the lightning actor, if I am understanding you correctly that is
  • famekraftsfamekrafts Member, BASIC Posts: 834
    I am not spawning it. Using constrain attributes. That's the problem or else with spawn it was easy. Currently what seems to be working is self rotation of lightning actor to vector angle (player.x,player.y) but the problem is same when the player is say at about 40 y, the lightning rotates to directly opposite direction and as the y increases it rotates towards the player. I will show the attributes screenshot and a diagram of what is happening.
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Image and video hosting by TinyPic

    another actor is making the fire true or false for every 5 secs and after 5.6 seconds..
  • famekraftsfamekrafts Member, BASIC Posts: 834
    this is the problem.

    Image and video hosting by TinyPic
  • ashtmjashtmj Member, PRO Posts: 405
    edited November 2012
    @wickedsunny I'll look this over in a bit, but in the meantime try this, I just put it together real quick, so there is some tweaking to do for sure https://www.sendthisfile.com/myfiles/filedetails.jsp?t=LdG8QTPV2NFQ7DHcq972L1wz

    walk with arrow keys, use jet pack with space bar
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Thanks but the problem is spawning. Let me see if I can use this in some way.
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Not working buddy. When I use your cos and sin values the cannon jumps near the player. Do we have to give some specific values?
  • famekraftsfamekrafts Member, BASIC Posts: 834
    I think I have got my graphics screwed up, the lightning image is imported as vertical while I am using actor as horizontal, maybe that is the issue with angle being opposite. Let me check once.
  • ashtmjashtmj Member, PRO Posts: 405
    I used cos/sin so the cannon would have a center point to spin off of, that way it isn't rotating from it's own center. if you just turn off the sin/cos constrains it will spin from the center.
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    edited November 2012
    Yep an actors defined starting rotation point is fixed. Put an arrow image in an actor and rotate it and view the angle with display text and you will see. I believe it goes like this top turning clockwise 0,90,180,270 and back to 360 or otherwise 0.
  • famekraftsfamekrafts Member, BASIC Posts: 834
    Yup the graphics was the problem, it was supposed to be horizontal instead of vertical. God my one day gone after this silly mistake. Thanks for all your help.
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    Yep been there long ago when I was learning GS. I should do a short video on that.
  • Yovanny369Yovanny369 Member Posts: 105
    This might just help you.
    Download it quickly!

    https://www.dropbox.com/sh/9gqiviyqfbjs7xz/Me9jyf_C3_
  • famekraftsfamekrafts Member, BASIC Posts: 834
    If I am using one attribute like player. X with my cannon, can I use it with another actor? Like I want a bomb to hurl towards the player also. Do I need to create a new constrain attribute, because the old one doesn't seem to work?

    Eduard0369 thanks, but my that problem got solved.
  • Yovanny369Yovanny369 Member Posts: 105
    Cool!
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    As long as you're only reading from the existing attribute yes.
Sign In or Register to comment.