Entity appear then gone

@Mason_Rocchio Every entity has an .enabled property that can be manipulated by code. You could move the entity to the player and use a timer to enable or disable to your liking. Here is a link to the scripting commands that are possible.

ScriptComponent | PlayCanvas API Reference

I know that but how would I get it to go to the player behind it every 20 seconds

@Mason_Rocchio I think you would have a separate script which could be placed in root. This script will be a sort of game manager. You would need to add two attributes. One for Player and another for the object you will want to move. In the update function of this script, you could look at player movement and adjust the position of the other object by using an offset of the player position. Are you looking for some type of AI for this object?

@Tirk182 ok but would I have to use a timer

@Mason_Rocchio In the update function it passes to you a (dt) value which is based on frame rate. This time variable can be used to get time for you and handle your timer. Here is a quick reference to what I trying to say.

Anatomy of a script | Learn PlayCanvas

There are some topics on the forum where you can see how to create a timer.

2 Likes

I am going to use the PlayCanvas timer script tutorial for the timer every 20 seconds. I just need help getting it to appear behind the player. So what would I type to get it to go behind the player?

You can get the position of the player and add an extra value on a specific axis.

2 Likes

so would I use a variable


Example.prototype.moveToRandomPosition = function () {
    this.entity.findbyname('player').getposition();
};
Example.prototype.moveToRandomPosition = function () {
    var pos = this.entity.findByName('player').getPosition();
    enemyEntity.setPosition(new pc.Vec3(pos.x + 2, pos.y, pos.z + 2));
};
1 Like

Would I create a attribute for the enemyEntity?

it say can not read properties of null (getPosition)

Make sure the name of the entity you try to find is exactly the same as the name of the entity in your scene.

it is Player and in the script it is Player it is the same

I just wanted to double check something from the example above:

Example.prototype.moveToRandomPosition = function () {
    var pos = this.entity.findbyname('player').getposition();
    enemyEntity.setPosition(new pc.Vec3(pos.x + 2, pos.y, pos.z + 2));
};

You will want to make sure that the findByName() and getPosition()functions are correctly in lower camel-case. That line should look e a bit more like:

var pos = this.entity.findByName('player').getPosition();

Can you confirm that your code looks like the line above?

1 Like

My bad, I just copied and pasted the code. :man_facepalming:

I updated my example above.

Screenshot 2022-10-26 8.33.53 AM

this is my code

this is the error I get

Below is my timer script from the PlayCanvas Docs but I edited it

var Example = pc.createScript('example');
Example.attributes.add('durationSecs', {type: 'number'});
Example.attributes.add('enemyEntity', {type: 'entity'});


// initialize code called once per entity
Example.prototype.initialize = function() {
    this._paused = false;
    this._timerHandle = pc.timer.add(this.durationSecs, this.moveToRandomPosition, this);
    
    this.on('destroy', function() {
        pc.timer.remove(this._timerHandle);
    }, this);
};


// update code called every frame
Example.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_P)) {
        this._paused = !this._paused;
        this.app.timeScale = this._paused ? 0 : 1;
    }
    
    if (this.app.keyboard.wasPressed(pc.KEY_R)) {
        pc.timer.remove(this._timerHandle);
    }
};


Example.prototype.moveToRandomPosition = function () {
    var pos = this.entity.findByName('Player').getPosition();
    enemyEntity.setPosition(new pc.Vec3(pos.x + 2, pos.y, pos.z + 2));
};

I guess you have to replace line 31 of the code above with the line below.

var pos = this.app.root.findByName('Player').getPosition();

does the script go in root?

the script is in the enemyEntity right now and it says cant define enemyEntity
The attribute I added is enemyEntity