1. Hi all,

i want to make a npc follow my player but can't seem to get the position of the player inside the script of my npc.

i tried GetNode<player>("player"); And Getnode("player") as player; ( in C#)

but i get node not found but its there :(

my scene looks like this: * World: 1. tiles 2. player 3. npc

i don't want to place the npc as child of player because i want to make the npc move only if player is to faraway and make him catch up to the player .

in change the path to ../player the error is gone but code dont work player.position is empty?

here is the code to make npc move

public override void _Ready()
    {
        playerObject = GetNode("../player") as player; //Get the player
        
    }

    private void Move(){ 
        if(playerObject.Position != null) //Test print to check if position has a value.
            GD.Print(playerObject.Position);
            
        Vector2 direction = playerObject.Position - this.Position; // to check position of the player if negatief player is left
        Vector2 distanceBetween = new Vector2(Math.Abs(playerObject.Position.x - this.Position.x),  //check distance between player and npc
                                                 Math.Abs(playerObject.Position.y - this.Position.y)); 

        if(distanceBetween > maxDistance){ /* check if the distance between player 
                                                and npc is not greater than max ditance */
            
            if(direction.x < 0){
                speed -= speed;
                motion.x = Math.Max(-maxSpeed, -speed); //speed up to maxspeed
            }
            else{
                speed += speed;
                motion.x = Math.Min(maxSpeed, speed); //speed up to maxspeed

            }
        }
        else{
            motion.x = Mathf.Lerp(motion.x, 0, 0.3f); // to stop npc
        }
    }

    public override void _PhysicsProcess(float delta){

        motion = MoveAndSlide(motion);
    }

Ok my bad i forgot to update Move() every frame :3 also i wil post the code again so if somebody look this up don't just copy it the don't get wrong outputs , there are some errors in the code that makes the npc disappear because its position becomes NAN :#

 private void Move(){ 
                    
        Vector2 distanceBetween = playerPosition.GetCurrentPosition() - this.Position; 

        if(Math.Abs(distanceBetween.x) > maxDistance.x){ /* check if the distance between player 
                                                and npc is not greater than max ditance */
            //GD.Print(distanceBetween);
            if(distanceBetween.x < 0){
                
                motion.x = Mathf.Lerp(motion.x, -maxSpeed, 0.1f); //speed up to maxspeed
               
            }
            else{
                
                motion.x = Mathf.Lerp(motion.x, maxSpeed, 0.1f); //speed up to maxspeed
                
            }
        }
        else{
                       
            motion.x = Mathf.Lerp(motion.x, 0, 0.1f); // to stop npc
           
        }

        if (Math.Abs(distanceBetween.y) > maxDistance.y)
        {
            if (distanceBetween.y < 0)
                motion.y = Mathf.Lerp(motion.y, -maxSpeed, 0.2f);
            else
                motion.y = Mathf.Lerp(motion.y, maxSpeed, 0.2f);
        }
        else
            motion.y = Mathf.Lerp(motion.y, 0, 0.2f);
    }
3 years later