Page 1 of 1

how do i make npc shoot stuff?

Posted: Thu Aug 05, 2021 7:09 am
by lolcode
I kinda know how to make npcs but i don't know how to make them spawn projectiles. I need the projectiles to have certain speed. Please help!

Re: how do i make npc shoot stuff?

Posted: Thu Aug 05, 2021 8:55 am
by Lucario
I was trying to code a Huckit Crab, and that was also my roadblock (I followed carefully Enjl's tutorial). So I'm sorry, I cannot help you. :(

Re: how do i make npc shoot stuff?

Posted: Thu Aug 05, 2021 9:33 am
by Emral
https://docs.codehaus.moe/#/reference/npc
If you look at the NPC.spawn function, you can see that it causes an NPC to spawn and returns the NPC in question.
local myNPC = NPC.spawn(id, x, y, section)
myNPC.speedX = 4

You can look at basegame code for (for example) the SMW bowser statue for reference.

Re: how do i make npc shoot stuff?

Posted: Thu Aug 05, 2021 6:33 pm
by lolcode
Enjl wrote:
Thu Aug 05, 2021 9:33 am
https://docs.codehaus.moe/#/reference/npc
If you look at the NPC.spawn function, you can see that it causes an NPC to spawn and returns the NPC in question.
local myNPC = NPC.spawn(id, x, y, section)
myNPC.speedX = 4

You can look at basegame code for (for example) the SMW bowser statue for reference.
thanks

Added in 1 hour 39 minutes 50 seconds:
btw, is it posible for a projectile to be launched exactly at the player?

Re: how do i make npc shoot stuff?

Posted: Fri Aug 06, 2021 12:57 am
by Emral
Yeah, just get the distance vector between the player and npc locations and normalize it so that it always has a length of 1:
local firingDirection = vector(player.x - npc.x, player.y - npc.y):normalize()

Re: how do i make npc shoot stuff?

Posted: Fri Aug 06, 2021 2:15 am
by lolcode
thank you