direction of the bullet.

Hello’s peepz,

I’m developing(trying to) a game where the player stands on a platform on the left side of the screen, enemies pop on the right side… player shoots at them. i want the bullets to go all the way to the direction of the shot and never stop unless it colides with something. so i cant use transition.to…

So im trying with setLinearVelocity(100,?). the problem is that with this function i cant really (or i dont know how) get the bullet to go the direction of event.y…

So for you guys that are probably smarter than me, i would greatly appreciate if someone could give me some light in regards to how i can go about achieving what i need.

Thank you in advance for reading at least…

Code more, play even more!! ) [import]uid: 149444 topic_id: 26664 reply_id: 326664[/import]

Hey there,

What event.y are you referring to?

If the player is facing right and the enemy is to the right of him/her you’d just set the x value.

Are your enemies at different heights (y) on the screen and you want the player to shoot automatically at an angle towards them?

Just trying to picture your setup :slight_smile:

Peach [import]uid: 52491 topic_id: 26664 reply_id: 108621[/import]

Hey,

thanks for replying.

Thats right. i have the player on the left side of the screen, facing the right side. as the enemies pop up from different (random) y, they will stop at about display.contentWidth/2 and start shooting at the player. so the player has to move up and down (y) to dodge the attacks and still be able to shoot at the direction of touch.

I managed to make it work by using applyForce with some crazy equation.*
*
local multiplier = -1
applyForce((bullet.x - touch.x)* multiplier, (bullet.y - touch.y)*multiplier)

The problem is that i cant use the same method to make so that the enemies attack when their transition to mid screen ends.

if i do the enemies simply shoot at some weird directions that by manipulating the multiplier and testing just didn’t do it…
I know it probably looks a bit complicated if you’re not looking at the (loong page of) code.

So if u can’t “see” the situation don’t worry about it, i will figure it out.

What happened is that i posted this while i was stuck with on problem, now im stuck at a different one and trying to use the “attention” to get some light… :stuck_out_tongue: [import]uid: 149444 topic_id: 26664 reply_id: 108636[/import]

OK so the issue is that the enemies shoot but the bullets don’t go where you want them to because the player is moving - is that correct?

Is it the equation to move the bullets that is the key issue now? How are you currently moving them? Force, transition, etc? [import]uid: 52491 topic_id: 26664 reply_id: 108796[/import]

So basically for the player to shoot, im just feeding to applyForce the position for each instance of a bullet and the position of touch. and it works fine.

For the enemies to shoot at the player however, i cant(dont want to) use transition because the whole idea is that the player would dodge the enemies’s bullets and transition would kinda cause the bullets to “follow” the players position. which would feel very weird.

So i think i need to use the same method… the issue is that that equation* is causing the enemies to shoot at weird directions and never at the current position of the player if u know what i mean…
multiplier = -1 (i tried changing the values here)
*applyForce(enemyBullet.x - player.x) * multiplier,enemyBullet.y - player.y) * multiplier)

This works perfectly for player shooting at desired direction but not the other way around… :confused: [import]uid: 149444 topic_id: 26664 reply_id: 108871[/import]

That actually appears to work fine doing a quick test - try running this;
[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

local player = display.newCircle( 20, 20, 20 )
physics.addBody(player, “static”)

local function shoot()
enemyBullet = display.newCircle(300, math.random(20,460), 10)
physics.addBody(enemyBullet)
multiplier = -1
enemyBullet.isBullet = true
enemyBullet:applyForce((enemyBullet.x - player.x) * multiplier, (enemyBullet.y - player.y) * multiplier)
end
timer.performWithDelay(500, shoot, 10)

transition.to(player, {time=4000, y=460})[/lua]

The player is static and the bullets just bounce off of course but it seems solid to me.

Thoughts?

Peach :slight_smile: [import]uid: 52491 topic_id: 26664 reply_id: 108898[/import]