Entity follow another?


(Oli) #1

how should I go about to do that?


(Jacob Albano) #2

You’re going to have to be more specific than that.


(Oli) #3

hmmm for instance there is a thread where this guy talks about the entity following the mouse position in the world but what if it followed the player entity instead.

This is the code for the mouse movement follow. x = world.mouseX; y = world.mouseY;

its pretty simple to implement

and btw i played your games and my favorite one is the cave one, its pretty cool


(Bora Kasap) #4

Need to know: *Do you want to lock an Entity to another like locking an Entity to your cursor. *Or do you want an Entity to follow another from back by using “same path” with another one. *Or do you want an Entity to follow another by using target locked basic Homing Missile tech?

But, it looks like You want Entity moving towards mouse continuously? But now, need to know, do you want smooth or direct?


(Oli) #5

direct would be better in my case but isnt that just a mather of adjusting the speed?


(Bora Kasap) #6
import net.flashpunk.FP;

targetangle = FP.angle(x, y, target.x, target.y)
x += Math.cos(targetangle * FP.RAD) * customspeed * FP.elapsed;
y += Math.sin(targetangle * FP.RAD) * customspeed * FP.elapsed;

(Oli) #7

cool I’ll try it out! thx:)


(Bora Kasap) #8

your welcome. don’t forget to stop follower when reached to target :smiley:


(Oli) #9

im sry but what do you mean by target angle?


(Bora Kasap) #10

there is an angle based movement vector in this sample. so you have to change angle every moment to change the direction continuously


(Oli) #11

wait so should I put a number in the “targetangle”? and where it says “target” the type of the entity I want to follow?


(Oli) #12

package entities { import net.flashpunk.Entity; import net.flashpunk.FP; import entities.Player import net.flashpunk.utils.Input

public class collisionplatform extends Entity
{
			
	public function collisionplatform() 
	{
	
	setHitbox(50, 10);
	type = "platform"
	super();			
	}
	
	override public function update():void
	{
	targetangle = FP.angle(x, y, target.x, target.y)
	x += Math.cos(targetangle * FP.RAD) * 10 * FP.elapsed;
	y += Math.sin(targetangle * FP.RAD) * 10 * FP.elapsed;
	super.update();	
	}
	
	
}

}

This is what I got so far.


(Bora Kasap) #13

no, the function calculates it self. you just have to put target.x and target.y


(Bora Kasap) #14

but there is a problem here, you need to use this function in “follower”. not in platform entity.


(Oli) #15

i an kind of confused sry, the platform follows the main player, so are you saying I should make another entity called follower?


(Oli) #16

Its ok nevermind man Ill try to figure out on my own. Sryfor wasting your time… but thx:)


(David Williams) #17

The way I read what you want, there could be two separate things you’re trying to do.

  1. Have the exact same coordinates as another entity. For this, you could just store reference to the entity you would like to ‘stick with’ and set the follower entity’s x and y to be the same as the referenced entity.
  2. Or, move towards another entity at a set speed. For this, you could use the FP.stepTowards(); function, or move along an angle, like what it appears you are currently trying to do. Here’s the code I use for that:

Code:

angle = FP.angle(this.x, this.y, target.x, target.y);
yVeloc = speed * Math.cos(angle * Math.PI / 180);
xVeloc = speed * Math.sin(angle * Math.PI / 180);

y -= yVeloc;
x += xVeloc;

(Oli) #18

Kk I will look this up when Im doing the AI later but for now I think if found another method


(Oli) #19

Do you use trigonometry when you do instant following too? like for instance locking an entity on another like you said at the begining.


(Bora Kasap) #20

of course not, that doesn’t need a function… x=n y=n… no more…