Entity follow another?


(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…


(Oli) #21

oh rly? cool and what does “n” means?


(Jacob Albano) #22

In programming and mathematics, n is a generic name for a given number. It can mean anything.

x = entityToFollow.x;
y = entityToFollow.y;

(Oli) #23

ok thank you for that:)


(Zouhair Elamrani Abou Elassad) #24

Could you please provide the code you used to follow to entity, i’m currently trying to follow the mouse but nothing works so far !


(Jacob Albano) #25

(Zouhair Elamrani Abou Elassad) #26

But if i did that, every time the player will be at the same position of the target not following, or am i wrong ?


(Martí Angelats i Ribera) #27

It will depend of the sysytem you use to move.


(Jacob Albano) #28

That’s what the original topic was about. If you have something else in mind, create a new thread.


(Zouhair Elamrani Abou Elassad) #29

Actually i already did, Thanks :smile: