How do I make a sprite follow a sprite and hit it?

0 favourites
  • 9 posts
From the Asset Store
12 unique hits, slashes and spells in transparent PNG sequences
  • I'm trying to make the bullet auto lock on and hit the target. I've tried using a while loop with the bullet behavior enabled and constantly setting the angle of the bullet towards the monster, until the bullet comes into collision with the monster, but as soon as I click on the monster my game crashes.

    I've attached a picture of my problem as I cant use the

    tag

  • Kill both 12 and 13 sub-events.

    Make an event (NOT sub-event) Bullet -- On collision with... Monster --> Bullet.Destroy

    Make an event (NOT sub-event) System -- Every tick --> Bullet -- Set angle Towards Monster.X, Monster.Y

    Why Bullet spawns Bullet? I think Player must spawn Bullet.

    Mmm.. try it. I think it will be more correct.

  • sKill both 12 and 13 sub-events.

    Make an event (NOT sub-event) Bullet -- On collision with... Monster --> Bullet.Destroy

    Make an event (NOT sub-event) System -- Every tick --> Bullet -- Set angle Towards Monster.X, Monster.Y

    Why Bullet spawns Bullet? I think Player must spawn Bullet.

    Mmm.. try it. I think it will be more correct.

    This is a good fix if I only had one monster. The reason why I had it in sub-events is because it kept the same UID for the monster that was clicked on. Now your way is good but i need it to lock on to the monster that was clicked, how do I go about doing that?

  • so you need to understand what bullet is for what monster

    you can pick needed monster by his IID (for example)

    every bullet - some var instance, ex. MonsterID

    after shot make Bullet.MonsterID = needed Monster.IID

    then in cycle for every bullet you need to pick monster with IID = bullet.MonsterID and then Bullet -- set angle towards -- monster.X, monster.Y

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • so you need to understand what bullet is for what monster

    you can pick needed monster by his IID (for example)

    every bullet - some var instance, ex. MonsterID

    after shot make Bullet.MonsterID = needed Monster.IID

    then in cycle for every bullet you need to pick monster with IID = bullet.MonsterID and then Bullet -- set angle towards -- monster.X, monster.Y

    Thanks I've fixed the problem. I didn't need the bullet ID as only one bullet will be on the screen at one time. But do you know how I can differentiate the monster thats colliding with the bullet?, as I've got multiple monster sprites on the screen.

  • emm.. when you check collision between monster and bullet, monster will be picked!

    On bullet collision with monster
    monster - Destroy[/code:h6mocc86]
    no need to pick this monster specially
    
    must work
  • emm.. when you check collision between monster and bullet, monster will be picked!

    On bullet collision with monster
    monster - Destroy[/code:gtz6hdlu]
    no need to pick this monster specially
    
    must work
    

    I think my explanation was a bit dodgy. I have got:

    On bullet collision with monster
    bullet - Destroy[/code:gtz6hdlu]
    
    But because I have multiple monster sprites running around, when the bullet is fired it is meant to destroy itself when it hits the monster that was clicked, however if another monster sprite is in the line of fire the bullet will be destroyed at that monster. I want the bullet to be destroyed when it hits the one I clicked on.
  • now.. I understand.. I think..

    you want to kill just monster you clicked on, even if bullet will fly through hordes of other monsters to reach your aim? we need to kill just clicked one and save others, yes?

    ok, let's do something like that:

    create global var ClickedMonsterID first

    I don't know you want to use mouse or touch, but let's use touch, it can simulate mouse by default

    Touch -> On tap monster
         ClickedMonsterID = monster.IID (or UID, no big difference for our example I think)
         <launch bullet with your parameters>
    
    On bullet collision with monster
         If monster.IID (or UID) = ClickedMonsterID
              bullet.Destroy
              monster.Destroy[/code:2wmzfc9o]
    of course, it can slow down game performance because of too many collision checks.
    
    Another way is better on my look but not so good because of one moment..
    So.. take all your monsters and toggle off [i]Collision[/i] property (to Disable value).
    And then code will be much easier:
    
    [code:2wmzfc9o]Touch -> On tap monster
         monster - Set collisions enabled = Enabled
         <bullet fire>
    
    On bullet collision with monster
         bullet.Destroy
         monster.Destroy[/code:2wmzfc9o]
    So, there will be just one monster who can collide with bullet.
    I don't know will it work or not, but you may try it. But you can use it only if you never need constant check of any other monster collisions (with obstacles for example).
    
    Try both and say your opinion.
  • now.. I understand.. I think..

    you want to kill just monster you clicked on, even if bullet will fly through hordes of other monsters to reach your aim? we need to kill just clicked one and save others, yes?

    ok, let's do something like that:

    create global var ClickedMonsterID first

    I don't know you want to use mouse or touch, but let's use touch, it can simulate mouse by default

    Touch -> On tap monster
         ClickedMonsterID = monster.IID (or UID, no big difference for our example I think)
         <launch bullet with your parameters>
    
    On bullet collision with monster
         If monster.IID (or UID) = ClickedMonsterID
              bullet.Destroy
              monster.Destroy[/code:38a1wj3u]
    of course, it can slow down game performance because of too many collision checks.
    
    Another way is better on my look but not so good because of one moment..
    So.. take all your monsters and toggle off [i]Collision[/i] property (to Disable value).
    And then code will be much easier:
    
    [code:38a1wj3u]Touch -> On tap monster
         monster - Set collisions enabled = Enabled
         <bullet fire>
    
    On bullet collision with monster
         bullet.Destroy
         monster.Destroy[/code:38a1wj3u]
    So, there will be just one monster who can collide with bullet.
    I don't know will it work or not, but you may try it. But you can use it only if you never need constant check of any other monster collisions (with obstacles for example).
    
    Try both and say your opinion.
    

    Thanks man! I used aspects of your first suggestion to get it working. I can't use the second one even though is less performance impacting as the monsters need collisions enabled at all times

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