Rigidbodies Local

I’m trying my enemy (Hulk) to look at me and follow me. I’m able to do it using the lookAt function, translateLocal and RotateLocal functions, However, the enemy, even though it’s a rigidbody, does not collide with other rigidbody objects. When I try to use the applyForce codes, it only moves on the world Axis and the lookAt function does not work anymore. I tried using the teleport and setLocalPosition codes but it does not seem to work. Attached are the code below, including all the options that I’ve tried which have been commented. Thanks

Playcanvas Link: PlayCanvas | HTML5 Game Engine

My Code:

var HulkFollowsPlayer = pc.createScript('hulkFollowsPlayer');

// initialize code called once per entity
HulkFollowsPlayer.prototype.initialize = function() {



    this.thePlayer = this.app.root.findByName("Player"); 

};

// update code called every frame
HulkFollowsPlayer.prototype.update = function(dt) {


/*
//OPTION 1 WITH BUG 
  //hulk should lookat the Player
  this.entity.lookAt(this.thePlayer.getPosition()); 

  this.entity.rotateLocal(0,180,0); 
 this.entity.translateLocal(0,0,.03); 
  //hulk enemy just to follow the x and z postition
  var EnemyPosition = this.entity.getPosition(); 
  //change or set the local Y position of hulk to a constant value
   this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z); 
//------------------*/ 



//OPTION 2 EXPERIMENT 

 

 this.entity.lookAt(this.thePlayer.getPosition()); 

this.entity.rotateLocal(0,180,0); 
 
 this.entity.translateLocal(0,0,.03)
// this.entity.setLocalEulerAngles(0,180,0);

 //var force = this.entity.forward.clone().scale(-3);
   //     this.entity.rigidbody.applyForce(force);

 
  
  //this.entity.rigidbody.linearVelocity(0,0,3);
 //this.force.copy(this.entity.forward).scale(-3);
// this.entity.translateLocal(0,0,.03); 
  //hulk enemy just to follow the x and z postition
 
  //var EnemyRotation = this.entity.getLocalR 

 //this.entity.rigidbody.applyForce(0,0,3); 

 var EnemyPosition = this.entity.getLocalPosition(); 

  
  //change or set the local Y position of hulk to a constant value
  //this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03); 



  //this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03); 
  //this.entity.rigidbody.teleport(EnemyPosition.x, EnemyPosition.y, EnemyPosition.z + .03); 
  


};

// swap method called for script hot-reloading
// inherit your script state here
// HulkFollowsPlayer.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/

Hi @ryekis and welcome!

With these methods you only move the visual entity and not the dynamic rigidbody of it. Thats why there is no collision anymore at the position where you expect it after using one of these methods.

You basically only can move or rotate an entity with a dynamic rigidbody using teleport(), applying a force or manipulate the rigidbody directly on another way.

I see you currently don’t use a dynamic rigidbody for your enemy entity. Be aware there is no automatic interaction with other objects when you use a kinematic rigidbody. You need to detect and react on collision by script manually.

I’ve already set it to Dynamic before and using the teleport function. But it still does not work. I’ve tried using getLocalPosition and getPosition but it still does not my work.

Here’s the new code. As you can see in the previous code, I’ve tried all options even if my enemy is set to a dynamic rigidbody :

var HulkFollowsPlayer = pc.createScript('hulkFollowsPlayer');

// initialize code called once per entity
HulkFollowsPlayer.prototype.initialize = function() {



    this.thePlayer = this.app.root.findByName("Player"); 

};

// update code called every frame
HulkFollowsPlayer.prototype.update = function(dt) {


/*
//OPTION 1 WITH BUG 
  //hulk should lookat the Player
  this.entity.lookAt(this.thePlayer.getPosition()); 

  this.entity.rotateLocal(0,180,0); 
 this.entity.translateLocal(0,0,.03); 
  //hulk enemy just to follow the x and z postition
  var EnemyPosition = this.entity.getPosition(); 
  //change or set the local Y position of hulk to a constant value
   this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z); 
//------------------*/ 



//OPTION 2 EXPERIMENT 

 

 this.entity.lookAt(this.thePlayer.getPosition()); 

this.entity.rotateLocal(0,180,0); 
 
 //this.entity.translateLocal(0,0,.03)
// this.entity.setLocalEulerAngles(0,180,0);

 //var force = this.entity.forward.clone().scale(-3);
   //     this.entity.rigidbody.applyForce(force);

 
  
  //this.entity.rigidbody.linearVelocity(0,0,3);
 //this.force.copy(this.entity.forward).scale(-3);
// this.entity.translateLocal(0,0,.03); 
  //hulk enemy just to follow the x and z postition
 
  //var EnemyRotation = this.entity.getLocalR 

 //this.entity.rigidbody.applyForce(0,0,3); 

 var EnemyPosition = this.entity.getLocalPosition(); 

  
  //change or set the local Y position of hulk to a constant value
  //this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03); 



  //this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03); 
  this.entity.rigidbody.teleport(EnemyPosition.x, EnemyPosition.y, EnemyPosition.z + .03); 
  


};

// swap method called for script hot-reloading
// inherit your script state here
// HulkFollowsPlayer.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/

Hopefully you can fork the project and help me with the codes. Thanks :))

Line 35 and 37 are not compatible with a dynamic rigidbody.

I’m also not sure what you try to do on line 65.

Yes the problem is on line 65. I want Hulk to come toward me (the Player)…but it’s moving on the world axis not on the local Z axis…the same thing happens if I use the applyForce method

As for line 35…I just want hulk to face toward me :))

Currently it’s unclear to me if you use a kinematic or dynamic rigidbody.

Unfortunately, you need a workaround to achieve this with a dynamic rigidbody. Below an example project.

https://playcanvas.com/project/808772/overview/look-at-with-physics

Maybe you can do -0.03 to move it in the opposite direction.

You can also consider to use my Basic enemy AI.

I already tried making it a negative value…(-0.3) but it still doesn’t work. The enemy does not come toward me. So does that mean that the LookAt function is not compatible with a dynamic rigidbody so that the enemy is continuously looking at my first person controller Player and following me?? :((

It’s probably only working for the visual entity, so the rigidbody has still the same rotation.