I Need Help Programming A.I For A Game I'm Working On

Hi This Is My First Post, So I Am Trying To Figure Out How To Add A.I To A Hello Neighbor Fan Game I Am Working On. So For Example When You First Start The Game He Just Goes Around And Does Stuff Like Walking Around His House, But If He Sees You In His House He Chases You, And Every Time You Get Caught He Gets More And More Suspicious, If Anyone Could Help That Would Great!
Here Is The Project -
https://playcanvas.com/editor/scene/894558
We Got The Turning Mechanic Done!

Hello @00ferman2! I don’t know exactly how much experience you already have, but maybe you can start with do a raycast from the owner to the player. Check which object the raycast hit. If the raycast hit the player it means the owner can see the player. After that you can start with moving the owner in the direction of the player.

i would try it… if i understood it, but i am newer to javascript, so i don’t really understand it, but thank you trying to help.

maybe you could try to show what code i might have to use, or like an example project, if you could that would help a lot!

I’m not sure because i always have to test if my code is correct but it is something like this:

var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.player.getPosition());

if (result && result.entity.tags.has("Player")) {
    this.entity.lookAt(result.entity);
    // start moving
}

You have to add that code in the update of your ‘owner’ script.

so i tried the code and got a error about the this.app.systems text and i do not know what to do about it

I see you have add it between the initialize function and the update function. You have to add it in the update function.

Chase.prototype.update = function(dt) {
    var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.player.getPosition());
    if (result && result.entity.tags.has("Player")) {
        this.entity.lookAt(result.entity);
        // start moving
    }    
};

Yeah. Initialize will only run it in the first frame of the game. Update will, well, update the code each frame.

so i have updated it and now it does nothing no errors, but the neighbor will not move at all do you know how to fix this?

Now you have a second update function and no initialize. Thats not correct. And no, the neighbor will not start moving because this is only the check if we can see the player. You also have to add the tag Player to the player and the player need a collider and a rigidbody. You have to make a lot of steps to make an AI.

var Chase = pc.createScript('chase');

// initialize code called once per entity
Chase.prototype.initialize = function() {
  
};

// update code called every frame
Chase.prototype.update = function(dt) {
    var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.player.getPosition());
    
    // player is visible
    if (result && result.entity.tags.has("Player")) {

        // look at the player
        this.entity.lookAt(result.entity);

        // start moving
    }  
};

so i have changed the code and now it does this,


and says unexpected identifier

You have to be careful when writing your code. Compare your code with my code and fix the differences or copy and past all.

now it has no errors but from what i can tell the neighbor is supposed to look a t the player and he is not, so i am thinking i should change this.entity.lookAt(result.entity); to this.entity.lookAt(result.player); or something like that but i was also thinking this this.entity.lookAt(result.entity = Player); would this work?

Try:

this.entity.lookAt(result.entity.getPosition());

so kind of like this? this.entity.lookAt(result.entity.getPosition(Player));

No just:

this.entity.lookAt(result.entity.getPosition());

so i tried it and nothing is happening so what should i do?

We can try to check if the raycast is working as we want.
So we can try to draw a line between the neightbor and the player.
I have add a few things in the code.

var Chase = pc.createScript('chase');

// initialize code called once per entity
Chase.prototype.initialize = function() {
    this.player = this.app.root.findByName("Player");
};

// update code called every frame
Chase.prototype.update = function(dt) {
    
    // draw a line between neightbor and player
    this.app.renderLine(this.entity.getPosition(), this.player.getPosition(), new pc.Color(1,0,0,1));

    // check if player is visible
    var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.player.getPosition());

    // player is visible
    if (result && result.entity.tags.has("Player")) {

        console.log("Player is visible");

        // look at the player
        this.entity.lookAt(result.entity.getPosition());

        // start moving
    }
    else {
        console.log("Player is not visible");
    }  
};

I see we can’t see a red line so we go to check it with the console of the browser. I have add it in the code above. Press F12 in the Google Chrome browser to see the console.

so the thing is i use a chromebook so i do not have f1- f12 keys but i could see the console using ctrl shift i but i dont see much, so are you talking about the code editor because i can’t see much things that are different…