问题
What my game has is a character in a preset position in a 2d-game mobile(x=0.33, y=9.48, z=0). I have enemys that are spawn from different angles of the screen with a movement script. But i have to modify the script so it moves towards the character position always.
Tried to set the position of the enemy = with character position. but that doesnt work.
Anyone has any idea how can i do that?
here is the script:
///
//Speed - essential : true ?
let speed;
let enabled = false;
let phys;
function init() {
speed =4;
//let d = Math.sqrt( Math.pow((enemy.x-playerPos.x), 2) + Math.pow((enemy.y-playerPos.y), 2) );
}
function update(dt) {
dt = 1 / 60.0; // fixed delta time
let enemy = this.entity().position();
let player = this.scene().find('Actor')[0];
let playerPos = player.worldPosition();
let d = new Vec3(
playerPos.x - enemy.x,
playerPos.y - enemy.y,
playerPos.z - enemy.z
)
const length = Math.sqrt(d.x * d.x + d.y * d.y);
let dirTowardsPlayer = new Vec3 (
d.x / length,
d.y / length,
d.z / length
)
this.entity().setPosition(
dt * dirTowardsPlayer.x * speed,
dt * dirTowardsPlayer.y * speed,
dt * dirTowardsPlayer.z * speed);
log(dirTowardsPlayer)
}
function signal(name, value) {
enabled = value;
}
i am expecting the enemy to move towards the character preseted position
来源:https://stackoverflow.com/questions/58075898/how-can-i-make-the-enemy-go-to-the-character-position