Jump to content

how can I get this in Lua


tipforeveryone
 Share

Recommended Posts

Hi,

I want to make a small game in which playerObject can move and collect coins around it among stones (demonstrated in below images)
when player moves to a point, press a key to make every coins within its aura radius be collected (sucked to) by player except stones
and is there any way to get only the closest coin ?
Those stones and coins are randomly generated and not moving

any idea for this ? how to make it in lua? Thanks for helping :)

image.png.62e08664d219a54475eccd54457e8a08.png

Link to comment
Share on other sites

Shouldn't be too bad using the GetDistance function to get the distance between the player and any coin and if the distance is within the desired radius, pull the coins to you.  I would then use Point to point the coin to the player then Move it forward (toward the player).  That way, you can keep the coin moving toward the player even if the player is moving (by pointing and moving every turn).

Link to comment
Share on other sites

I thought about GetDistance() before. yes, that is only one solution for this, and using entity:Point() for pulling coins to player is brilliant.

But if there are multiple objects like player (may be a random enemy which wants to collect coins too then you must collect more coins than them to win) how can those coins use GetDistance() to all player like objects and only move toward the closest one?

Link to comment
Share on other sites

I would probably use a bounding box around each actor. You can get all coins that are within the box and tell them to move towards you.

https://www.leadwerks.com/learn?page=API-Reference_Object_World_ForEachEntityInAABBDo

Call this every so often in each Actors UpdateWorld() and for each coin entity that is in the bounding box tell it to move towards you. Maybe check that if it's already moving towards another actor to ignore moving towards another but maybe not, maybe that's part of the game.

Each actor should have an AABB object and you define it by setting it's center point to be the players position in the UpdateWorld() so it moves, and give it a radius of however big you want.

https://www.leadwerks.com/learn?page=API-Reference_Object_Math_AABB

This will probably be faster than GetDistance() checks on all coins by all actors.

  • Thanks 1
Link to comment
Share on other sites

If your coins don't need to interact with physics once they are moving towards you and you don't want them to rotate towards the player you can move them like this:

--UpdateWorld
local velocity = (playerPos - coinPos):Normalize() * self.coinMoveSpeed * Time:GetSpeed()
local newCoinPos = coinPos + velocity;
coin:SetPosition(newCoinPos)

 

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...