Set Bullet Angle Towards Another Object

Get help using Construct 2

Post » Sun Jul 07, 2013 9:33 am

Hey hey,
I am working on a platform game where the player collects coins/fruits etc. Upon touching the fruit I have enabled a bullet behavior on a set angle. This gives me a crude version of the effect I want.

What I want to know is how would I set the bullet's angle of motion towards another object? I have tried setting its angle to the windowwidth/windowheight etc but that doesn't work after I start moving away from the initial start area, for obvious reasons.

Basically, when an item is collected, I want it to snap to the top left hand corner and make the UI number go up by +1. Im sure most people here have seen this effect.

Any help is greatly appreciated.

Thank You,
Stephen
B
56
S
15
G
13
Posts: 826
Reputation: 17,705

Post » Sun Jul 07, 2013 10:06 am

Setting it to another object wouldn't work, as the UI object's actual position is always going to be off, owing to the UI layer's parallax property.
Assuming that you have your UI object in the top left corner, you can set the object's bullet angle to angle(Collectable.X,Collectable.Y,ViewportLeft("Game"), ViewportTop("Game")). This will just send it off to the top left corner and not, strictly speaking, to the UI object itself, but the effect is the same.
B
57
S
15
G
11
Posts: 912
Reputation: 12,626

Post » Sun Jul 07, 2013 7:03 pm

@Geometrix Okay, thanks for your help. I have implemented what you have shown me and I am getting the same result. I should have mentioned that I want the angle to update every frame.

You see if the player is running and collects something, the item will actually end up going off the screen lower than intended. The same goes for the player running the opposite direction. The collectable will end up going off the screen somewhere near the top/middle of the screen.

I am think I need create an Every Frame event, but I am not sure about how to go about this. I will play around with it in the mean time.

Thank You,
StephenTekniko2013-07-07 19:04:06
B
56
S
15
G
13
Posts: 826
Reputation: 17,705

Post » Sun Jul 07, 2013 8:47 pm

Add an invisible sprite in your "Game" layer and give it Pin behavior. On start of layout pin it to your UI element.
Change the fruit's behavior from bullet to custom movement and use "accelerate towards position". Set X,Y of your sprite and never look back ;-p



B
6
S
1
Posts: 17
Reputation: 459

Post » Sun Jul 07, 2013 10:39 pm

[QUOTE=Tekniko] @Geometrix Okay, thanks for your help. I have implemented what you have shown me and I am getting the same result. I should have mentioned that I want the angle to update every frame.

You see if the player is running and collects something, the item will actually end up going off the screen lower than intended. The same goes for the player running the opposite direction. The collectable will end up going off the screen somewhere near the top/middle of the screen.

I am think I need create an Every Frame event, but I am not sure about how to go about this. I will play around with it in the mean time.

Thank You,
Stephen[/QUOTE]

Could you not create the object on a layer with its Parallax X and Y set to 0? That way the object's position would be independent of your movement layer. If you have a HUD layer, you could just move the object to that layer with Move to Layer, and you'd only have to calculate the angle once.

Seems a bit more economical than constantly readjusting the angle.
B
3
Posts: 23
Reputation: 221

Post » Sun Jul 07, 2013 11:53 pm

This is what I am now using. I need to set the speed up to 10,000 just to get the fruit to move off the screen in under 2 seconds. I have pinned an object to my Game layout and set the paralax to 0,0 I can see it scrolling along with me. But the Fruit still try to go to 0,0 of the entire layout.


The .capx fileTekniko2013-07-08 02:27:36
B
56
S
15
G
13
Posts: 826
Reputation: 17,705

Post » Mon Jul 08, 2013 12:41 am

[QUOTE=Tekniko] This is what I am now using. I need to set the speed up to 10,000 just to get the fruit to move off the screen in under 2 seconds. I have pinned an object to my Game layout and set the paralax to 0,0 I can see it scrolling along with me. But the Fruit still try to go to 0,0 of the entire layout.


https://www.dropbox.com/s/fslf5xkj8b5iwr0/FruitMovement.capx[/QUOTE]

Unfortunately, I use an older version of Construct 2 so can't view your capx. I would suggest perhaps posting screenshots of the relevent code, rather than your entire game though. Unless you don't mind sharing your code of course.

Where is it that you want it to go exactly?

I haven't used custom movement myself. but once it reaches sprite.x, sprite.y, shouldn't you set the acceleration to zero?

You could do this by creating a new event to check when the object is at position sprite.x, sprite.y, or when the object has collided with the sprite object, then set acceleration to 0. Or disable the custom movement entirely.Nullus2013-07-08 01:13:27
B
3
Posts: 23
Reputation: 221

Post » Mon Jul 08, 2013 1:49 am

You have to move the fruit to the GUI layer. And bullet seems to work better than custom movement.
fruit_movement.capx (r137)
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,680


Return to How do I....?

Who is online

Users browsing this forum: dop2000, NN81, work3 and 6 guests