How do I position target sprite to edge of another sprite? C2

0 favourites
  • 13 posts
From the Asset Store
Vector illustration of a UFO with an alien in three different positions.
  • Hi Community,

    I have a player sprite with a circle sprite (over lapping player)... it position is set to match player's movement.

    The circle is much bigger that the player... like a radius.

    I also have a target sprite sitting idle.

    What i'm trying to do is... detect if Enemy sprite collides/overlapping with circle... position the target sprite on the edge of the circle - where the Enemy is. then I can set angle of bullets to the target.

    How to achieve this please?

    It's the setting target to edge of the circle that's got me puzzled.

    Thanks,

    Hope this makes sense.

    Roberto

  • Okay, after experimenting, I kinda got something.

    On Collision with circle | Targ / Set position to (circle.Angle + Enemy.x, circle.Angle + Enemy.y)

  • What, why do you add up an angle with x/y coordinates? It probably works, because the circle.angle is zero, but it doesn't make any sense.

    You need this:

    Circle on collision with Enemy
    	Target set position to Player
    	Target move at angle
    		Angle: angle(Player.x, Player.y, Enemy.x, Enemy.y)
    		Distance: (Сircle.width/2)
    

    If multiple enemies can be in range, you might want to modify it to something like this:

    Circle is overlapping Enemy
    Enemy pick nearest to Player.x, Player.y
    
    	Target set position to Player
    	Target move at angle
    		Angle: angle(Player.x, Player.y, Enemy.x, Enemy.y)
    		Distance: (Сircle.width/2)
    

    Also, you can do the same without the circle sprite, using Line of Sight behavior.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks dop2000,

    Simply amazing! works great.

    Thanks again,

    Roberto

  • Hi dop2000,

    Can i pick your brains about this please?

    I'm trying to add an Elseif in the example you provided. basically, i have a spawn object that now spawns the Enemy.

    Circle is overlapping Enemy....................Target set position to Player

    Enemy pick nearest to Player.x, Player.y.......Target move at angle

    ...............................................Angle: angle(Player.x, Player.y, Enemy.x, Enemy.y)

    ...............................................Distance: (Сircle.width/2)

    ELSE

    Circle is overlapping Spawner..................Target set position to Player

    Enemy pick nearest to Player.x, Player.y.......Target move at angle

    ...............................................Angle: angle(Player.x, Player.y, Spawner.x, Spawner.y)

    ...............................................Distance: (Сircle.width/2)

    ELSE

    TARGET set position (as I want it to kind of follow the player, rather than being idle off screen)

    Hope this makes sense... the above doesn't work form me :(

  • You need to organize the code correctly using sub-events:

    1 Circle is overlapping Enemy....Target set position to Player
    
    subevent:
    2	Enemy pick nearest to Player.x, Player.y
    	....Target move at angle(Player.x, Player.y, Enemy.x, Enemy.y), Distance: (Сircle.width/2)
    
    
    3 ELSE (it should be at the same level as event #1)
     Circle is overlapping Spawner...Target set position to Player
    
    subevent:
    4	Spawner pick nearest to Player.x, Player.y
    	.....Target move at angle(Player.x, Player.y, Spawner.x, Spawner.y), Distance: (Сircle.width/2)
    
    
    
    5 ELSE (again at the same level as #1 and #3)
    	....TARGET set position to (somewhere else)
    
    

    If this doesn't help, please post your project.

  • It make's sense now - Thanks dop2000

  • Hi dop2000,

    !!! This is the last query on this topic - I promise!

    If I just wanted the target the sit on edge of circle and basically follow the player's direction.

    Target set position to Player
    	Target move at angle
    		Angle: angle(Player.x, Player.y, circle.x, circle.y)
    		Distance: (Сircle.width/2)
    

    This works, but it's a little flaky, when moving - i.e. The target jumps around when moving.

    Thanks,

    Roberto

  • What do you mean by follow? Follow behind the player, or above the player? Is this a top-down view game? Which behavior are you using for player movement?

    If you set the circle to player's position, or pin it to the player, then of course your code will not work, because there is no angle between these two sprites.

  • Hi,

    It's a top down game with 8Direction on player. and I guess, the target is always in front of the player... if player's angle is 0, the target is 0.

    Your second comment is interesting.

  • I guess, just pinning it is the easiest solution. Thanks dop2000.

  • To set the target in front of the player you can modify your code like this:

    Target set position to Player
    	Target move at angle
    		Angle: Player.8Direction.MovingAngle
    		Distance: (Сircle.width/2)
    

    Or you can simply pin the target, if you don't need to auto-aim it at enemies.

  • That's great - Muchos Gracias dop2000

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