0

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

filutu
  • 11
  • 2
  • The vector towards the player is `player - enemy`. Or `(player.x - enemy.x, player.y - enemy.y)` in a 2D game. This vector is typically adjusted to the appropriate length, by dividing it by its length to normalize it, then multiplying it with the per frame speed. –  Sep 24 '19 at 08:21
  • Hey, Chris! Thanks a lot for the reply! but how do i do that? Sorry but i am noobish in gaming development – filutu Sep 24 '19 at 08:35
  • What i achived till now is to calculate the distance between character and enemy. But i cant figure out how can i make the enemy move towards that position – filutu Sep 24 '19 at 08:37
  • Say you've subtracted the enemy position from the player position, and the result is `dx = 30` and `dy = 40`. The `length` is now `Math.sqrt(dx * dx + dy * dy)`, here `50`. `speed` is something like `5`, a simple number indicating "pixels per frame". So the new x is `pos.x + dt * dx * speed / length` and the new y is `pos.y + dt * dx * speed / length`. –  Sep 24 '19 at 08:43
  • i am sorry but my mind is still dizzy and i cant seem to wrap my head around it :( thx for helping tho! hug – filutu Sep 24 '19 at 09:02
  • Chris, i have it work it somehow but not how i want to. The enemy goes into only 1 direction. Just 1 more question. when u say the new x = pos.x, what is pos? enemy pos?. ``` – filutu Sep 24 '19 at 09:18
  • In your code, `pos` is the entity's position, so the enemy position, right? That is what changes, so add the movement vector to that, yes. I was referring to your `let npos = new Vec3(` code. –  Sep 24 '19 at 09:20
  • Thats how i did and it does not work :(. i will attach my new code down below – filutu Sep 24 '19 at 09:22
  • 1. Here's example code: https://jsfiddle.net/khrismuc/uzkdq0en/ 2. do not post that as answer; it's not an answer. If you have edited your code, put it in the *question* 3. "does not work" is a useless problem description. How exactly does it not work? What happens instead? Error messages? –  Sep 24 '19 at 09:24
  • Wait, there's a typo in there, one I had in my original comment but saw too late. `enemy.y + dt * dy * speed / length,` (**dy**, not dx). Same for `dz`. –  Sep 24 '19 at 09:27
  • Hey man. i implemented again by that example and by what you told me. But the enemy is not moving. It just sits in a point. Also, thanks for the edit code trick. didnt know xD – filutu Sep 24 '19 at 10:14
  • 1
    Nevermind, Chris!! it worked as it supposed!! THANKS you soo much – filutu Sep 24 '19 at 10:17
  • Possible duplicate of [Game enemy move towards player](https://stackoverflow.com/questions/2625021/game-enemy-move-towards-player) –  Sep 24 '19 at 10:41

0 Answers0