I am trying to make a game where a sprite has to follow food and eat it, so far I got the following code to work but I want the sprite to actually point to the closest food and follow it
I dont know how to make it follow the closest food, only all of it but it works sort of anyways
How do I make it point to the food? If possible also how would I make it move only to the closest piece of food?
// custom component controlling organism movement
export default function MoveToFood() {
return {
id: "MoveToFood",
require: ["pos", "area",],
add() {
//Organism move towards food
onUpdate("Food", (f) => {
dir = f.pos.sub(this.pos).unit();
this.move(dir.scale(Speed));
})
},
}
}