[SOLVED] Enemy script - Boss fight?

Hello! I am trying to make a boss fight for my game cursed life. But we already have a enemy script but it doesnt follow the player and I need it to stay put until it sees the player then it attacks. Any suggestions (and i think its coding but im not good at coding :frowning: )

Also is it possible to do cutscenes in playcanvas?

You could use colliders to detect if the player is in range, and yes cutscenes are possible.

If you dont mind me asking, what colliders can you show me an example? and what about the enemy script?

Also, is it possible if you can I can add you to my game and you can do it because me and my friend are clueless

(props to you @Fus_ion for being a 7th grader and being this good)

1 Like

To make a collider just make a entity and add a collision thats pretty much it and to get when the player goes through it you do:

this.entity.collision.on("triggerenter",myFunction,this);

And when they leave the collider:

this.entity.collision.on("triggerleave",myFunction,this);
2 Likes

ok then I do my enemy script? var Enemy = pc.createScript(‘enemy’);

Enemy.attributes.add(‘player’, {type : ‘entity’});
Enemy.attributes.add(‘speed’, {type : ‘number’, default : 10});
Enemy.attributes.add(‘damageTime’, {type : ‘number’, default : 3});
Enemy.attributes.add(‘damage’, {type : ‘number’, default : 10});
Enemy.attributes.add(‘orientation’, {type : ‘entity’});

// initialize code called once per entity
Enemy.prototype.initialize = function() {
this.damagetime = this.damageTime;
//this.entity.collision.on(‘contact’, this.onContact, this);

};

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

this.damagetime -= dt;


//movement
var from = this.entity.getPosition();
var to = new pc.Vec3(from.x, from.y - 1, from.z);
this.grounded = false;
var result = this.app.systems.rigidbody.raycastFirst(from, to);

if(result)
{
    this.grounded = true;
}
else
{
    this.grounded = false;
}

//Movement
var forward = this.orientation.forward;
var right = this.orientation.right;
var force = new pc.Vec3();
var rb = this.entity.rigidbody;


var source = this.entity.getPosition();
var target = this.player.getPosition();
var a = new pc.Vec3();
var direction = a.sub2(source, target);
var dir = a.sub2(source, target);







// use direction from keypresses to apply a force to the character
direction.normalize();
force.set(direction.x, 0, direction.z).normalize();
force.scale(this.speed);

//looking
this.orientation.lookAt(new pc.Vec3(target.x, target.y + 0.85, target.z));


this.entity.rigidbody.applyForce(direction.scale(-this.speed));



//this.text.element.text = this.entity.script.target.currentHealth;

};

Enemy.prototype.onContact = function(result) {

};

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

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

also, for the ```
this.entity.collision.on(“triggerleave”,myFunction,this);

do i put it in a script?

Instead of contact it should be ‘triggerenter’ and instead of making it the boss that checks if it is in range make a seperate child node that checks it.

Just replace the part of line 9 where it says contact with triggerenter

Then you should be good to go to make the boss follow :+1:

Enemy.prototype.onContact = function(result) {
 // follow goes here... Also the following as to be in a loop.
};
1 Like

var Enemy = pc.createScript(‘enemy’);

Enemy.attributes.add(‘player’, {type : ‘entity’});
Enemy.attributes.add(‘speed’, {type : ‘number’, default : 10});
Enemy.attributes.add(‘damageTime’, {type : ‘number’, default : 3});
Enemy.attributes.add(‘damage’, {type : ‘number’, default : 10});
Enemy.attributes.add(‘orientation’, {type : ‘entity’});

// initialize code called once per entity
Enemy.prototype.initialize = function() {
this.damagetime = this.damageTime;
//this.entity.collision.on(‘triggerenter’, this.onTriggerenter, this);

};

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

this.damagetime -= dt;


//movement
var from = this.entity.getPosition();
var to = new pc.Vec3(from.x, from.y - 1, from.z);
this.grounded = false;
var result = this.app.systems.rigidbody.raycastFirst(from, to);

if(result)
{
    this.grounded = true;
}
else
{
    this.grounded = false;
}

//Movement
var forward = this.orientation.forward;
var right = this.orientation.right;
var force = new pc.Vec3();
var rb = this.entity.rigidbody;


var source = this.entity.getPosition();
var target = this.player.getPosition();
var a = new pc.Vec3();
var direction = a.sub2(source, target);
var dir = a.sub2(source, target);







// use direction from keypresses to apply a force to the character
direction.normalize();
force.set(direction.x, 0, direction.z).normalize();
force.scale(this.speed);

//looking
this.orientation.lookAt(new pc.Vec3(target.x, target.y + 0.85, target.z));


this.entity.rigidbody.applyForce(direction.scale(-this.speed));



//this.text.element.text = this.entity.script.target.currentHealth;

};

Enemy.prototype.onTriggerenter = function(result) {

};

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

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/ so thats good now?

Yep now to make it follow.

wow ok now let me see in launch

oh meaning i have to do a script (idk how)

ahhh too much bugs!!!

https://playcanvas.com/editor/scene/1095384

var locked = false;
Enemy.prototype.onTriggerEnter = function(result) {
    locked = true;
};

Enemy.prototype.onTriggerLeave = function() {
    locked = false;
};

Enemy.prototype.update = function() {
    if(locked === 0){
        return;
    }else if(locked === !0){  
       var player = this.app.root.findByName("Player");
       var playerpos =  player.getPosition();
       if(player.rigidbody){
          // cant think of a idea for physics for now
       }else{
          this.entity.setPosition(this.entity.getPosition().lerp(this.entity.getPosition(),playerpos,pc.math.smooterstep(0,1,0.25)));
       }
    }
};
1 Like

ok where do I put that

or another script?

replace your update func with the new one.

1 Like