@albertos I need your help with this one! Thanks in advance.
In my Basic enemy AI example project I use some code to get a random position.
https://playcanvas.com/project/870482/overview/example--basic-enemy-ai
Thanks a million, but what about using onJoin() function to enable the player to a random position from 200 to 200?
Did you already found the function that I use to get a random destination for my enemy? You can find it in enemy.js script. Please copy the function and share it here.
Okay.
Here:
Enemy.prototype.randomDestination = function () {
var xPosition = pc.math.random(-this.enemyGround.getLocalScale().x/2, this.enemyGround.getLocalScale().x/2);
var zPosition = pc.math.random(-this.enemyGround.getLocalScale().z/2, this.enemyGround.getLocalScale().z/2);
this.enemyDestination.setPosition(xPosition, 1, zPosition);
};
I think it will be something like below.
// inside initialize function
this.setRandomPosition())
YourScriptName.prototype.setRandomPosition = function () {
// modify to your needs
var groundSize = 200;
var yPosition = 1;
// get random position
var xPosition = pc.math.random(-groundSize/2, groundSize/2);
var zPosition = pc.math.random(-groundSize/2, groundSize/2);
// apply random position
this.entity.setPosition(xPosition, yPosition, zPosition);
};
I just realize you use a dynamic rigidbody. You need to replace the last line of the function with the line below.
this.entity.rigidbody.teleport(xPosition, yPosition, zPosition);
I get this error when I try to run the script:
[RandomPosition.js?id=69891364&branchId=d58ee5d8-ab58-4e79-9fd4-cafe53c65450:4]: Uncaught TypeError: this.setRandomPosition is not a function
TypeError: this.setRandomPosition is not a function
at https://launch.playcanvas.com/api/assets/files/scripts/RandomPosition.js?id=69891364&branchId=d58ee5d8-ab58-4e79-9fd4-cafe53c65450:4:11
Here is the script:
var RandomPosition = pc.createScript('randomPosition');
// inside initialize function
this.setRandomPosition();
RandomPosition.prototype.setRandomPosition = function () {
// modify to your needs
var groundSize = 200;
var yPosition = 1;
// get random position
var xPosition = pc.math.random(-groundSize/2, groundSize/2);
var zPosition = pc.math.random(-groundSize/2, groundSize/2);
// apply random position
this.entity.rigidbody.teleport(xPosition, yPosition, zPosition);
};
As far I can see you didn’t add this.setRandomPosition()
to the initialize
function of the script.
And how would do that?
var RandomPosition = pc.createScript('randomPosition');
// initialize code called once per entity
RandomPosition.prototype.initialize = function() {
};
// inside initialize function
this.setRandomPosition();
RandomPosition.prototype.setRandomPosition = function () {
// modify to your needs
var groundSize = 200;
var yPosition = 1;
// get random position
var xPosition = pc.math.random(-groundSize/2, groundSize/2);
var zPosition = pc.math.random(-groundSize/2, groundSize/2);
// apply random position
this.entity.rigidbody.teleport(xPosition, yPosition, zPosition);
};
Is this good?
No, because this.setRandomPosition()
is below (outside) the initialize
function at the moment.
What is the correct way?
RandomPosition.prototype.initialize = function() {
this.setRandomPosition();
};
// MyRoom.ts
// ...
onJoin(client: Client, options: any) {
console.log(client.sessionId, "joined!");
// create Player instance
const player = new Player();
// place Player at a random position
const FLOOR_SIZE = 4;
player.x = -(FLOOR_SIZE/2) + (Math.random() * FLOOR_SIZE);
player.y = 1.031;
player.z = -(FLOOR_SIZE/2) + (Math.random() * FLOOR_SIZE);
// place player in the map of players by its sessionId
// (client.sessionId is unique per connection!)
this.state.players.set(client.sessionId, player);
}
// ...
}
This should work, right?
I have no idea.
LOL, I can actually code, just not very good.
So, @Albertos how could I add your network script so you can see other player’s rotation with my rotation script?
As I said in the other topic.