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

Get help using Construct 2

Post » Thu Mar 05, 2015 12:18 pm

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.

Image

I've attached a picture of my problem as I cant use the [Img] tag
You do not have the required permissions to view the files attached to this post.
B
3
Posts: 9
Reputation: 193

Post » Thu Mar 05, 2015 12:28 pm

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.
Last edited by NickRimer on Thu Mar 05, 2015 12:30 pm, edited 1 time in total.
B
8
Posts: 72
Reputation: 456

Post » Thu Mar 05, 2015 12:53 pm

NickRimer wrote: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?
B
3
Posts: 9
Reputation: 193

Post » Thu Mar 05, 2015 2:05 pm

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
B
8
Posts: 72
Reputation: 456

Post » Tue Mar 10, 2015 9:02 am

NickRimer wrote: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.
B
3
Posts: 9
Reputation: 193

Post » Tue Mar 10, 2015 1:38 pm

emm.. when you check collision between monster and bullet, monster will be picked!
Code: Select all
On bullet collision with monster
monster - Destroy

no need to pick this monster specially

must work
B
8
Posts: 72
Reputation: 456

Post » Wed Mar 11, 2015 2:55 pm

NickRimer wrote:emm.. when you check collision between monster and bullet, monster will be picked!
Code: Select all
On bullet collision with monster
monster - Destroy

no need to pick this monster specially

must work


I think my explanation was a bit dodgy. I have got:
Code: Select all
On bullet collision with monster
bullet - Destroy


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.
B
3
Posts: 9
Reputation: 193

Post » Wed Mar 11, 2015 9:14 pm

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

Code: Select all
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

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 Collision property (to Disable value).
And then code will be much easier:

Code: Select all
Touch -> On tap monster
     monster - Set collisions enabled = Enabled
     <bullet fire>

On bullet collision with monster
     bullet.Destroy
     monster.Destroy

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.
B
8
Posts: 72
Reputation: 456

Post » Wed Mar 18, 2015 9:43 am

NickRimer wrote: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

Code: Select all
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

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 Collision property (to Disable value).
And then code will be much easier:

Code: Select all
Touch -> On tap monster
     monster - Set collisions enabled = Enabled
     <bullet fire>

On bullet collision with monster
     bullet.Destroy
     monster.Destroy

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
B
3
Posts: 9
Reputation: 193


Return to How do I....?

Who is online

Users browsing this forum: Greaver, KADIX, Sean Noonan and 21 guests