[SOLVED] How to get one entity to follow another entity automatically?

I want to make a game where one box follows another box and kills it, but how do I get the AI enemy box to follow and collide with mine, I just need the script, I already added the rigid body and collision

1 Like

Hi,

Welcome! Check out the lookAt function. After you have your entity pointing in the right direction, apply force on the forward axis.

2 Likes

I tried but how do you specify the other entitys name??
This.entity.lookAt(RedBlock)
How do i name the other entity

You grab first a reference to that other entity and you pass its position to the lookAt method:

var redBlockEntity = this.app.root.findByName('RedBlock');
this.entity.lookAt( redBlockEntity.getPosition() );

You can check the docs for the pc.Entity class to see how these methods are defined and what arguments they expect.

https://developer.playcanvas.com/en/api/pc.Entity.html

Alright i will try that, but could u make a quick project with 2 squares on a base plate and one is red the other is Blue, and set the red one to follow the blue one where ever it is on the base and collide, i just want to see

You can check this tutorial on how to have an entity move towards a point in the 3D scene:

https://developer.playcanvas.com/en/tutorials/point-and-click-movement/

I want it to be automatic, no mouse or keys involved, all ai

You will have to teach yourself some programming to be able to add custom logic and functionality to your game project.

There are plenty of tutorials to look to and educate yourself, but you will have to get creative on how to combine what you learn with what you want to achieve.

Alright, ill try and keep u updated if i run into a syntax error or something like "cant read getPosition of null

1 Like

It said red is still undefined even tho i named it

Your Red variable is a local variable available only in the block statement (method context) where it was defined. You can’t reference it outside of that context.

To make a variable available in a broader context you usually defined it as a property to the script context, like this:

this.Red = this.app.root.findByName('Red');

That way you can use it in the other methods of the same script, but not so easily between different scripts. To make it simpler for you I’d say add it to the initialize method of the movee script and in your update method change the first statement to (without trying to access an .entity member property this.Red is already an Entity):

var currentPosition = this.Red.getPosition();
1 Like

Thanks so much for the support i got it, i really appreciate u Leonidas


Thats all that i used, though it would be much harder, also i adjusted the angular positions to 0 so my node would stand up right

That’s great! Now unnecessary calls / allocations in your update loop mean allocating new things in memory per frame. Quickly this can become a performance bottleneck.

Here is a way to rewrite your code, without changing the output but increasing performance:

Blue.prototype.initialize() = function(){
   this.Red = this.app.root.findByName('Red');
   this.force = new pc.Vec3();
};

Blue.prototype.initialize() = function(){
   this.entity.lookAt(this.Red.getPosition());
   this.force.copy(this.entity.forward).scale(15);
   this.entity.rigidbody.applyForce(this.force);
};
1 Like

Thx, ill be sure to use this, when i attach a model to my capsule, i want it to stay attached forever and rotate

with it but also be able to collide with, but if i put it on static, the capsule cant move if i do dynamic it falls off

You can try using kinematic objects then, you will have to change the way you move them (use setPosition instead of forces).

Check this manual page for more on that:

https://developer.playcanvas.com/en/user-manual/physics/physics-basics/

Nah theres an easier way

Now blue teleports to the other and not walk, why?

Sorry, I can’t understand your issue from this code. Could you share a sample project url?

1 Like

https://playcanvas.com/project/667080/overview/testing200

How do i get the wntity to move and rotate at the same time, whats the exact scrpt