0 votes

In my project there a enemy(position2D) node which instances obstacles.But as the player moves the enemy node is struck where it originally was. I tried making the enemy node child of player but I am getting error. Can anyone tell me how can i move the enemy(Position 2D) move ahead of the player such that as the player moves the enemy node is instanced creating obstacles as the player moves forward ?
Here is the Project

asked May 6 in Projects by Yasin (53 points)

1 Answer

+1 vote

You can check the position of the player, and add an offset to the y-value,
Add this to your enemy-script:

export var offset : float = -800

func _process(delta: float) -> void:
    var Player : KinematicBody2D = get_parent().get_node(("Player"))
    position.y = Player.position.y + offset

You should probably save a reference to the player-node in _ready and use that instead, so you dont have to check every _process-call.

I hope i could help you with that.

answered May 7 by whiteshampoo (976 points)

Can u check if this code is correct ; I made a few change at var pos and added add_random_obstacle(pos) .
On executing I am getting an error W 0:00:00:0382 The function 'addrandomobstacle()' returns a value, but this value is never used.

  extends Node2D

  export (Array, PackedScene) var obstacles

  var pos : Vector2 = Vector2()

  func _ready():
      assert(len(scenes))
      add_random_obstacle(pos)

  func add_random_obstacle(pos : Vector2) -> Node2D:
      return add_obstacle(floor(rand_range(0, len(obstacles))), pos)

  func add_obstacle(type : int, pos : Vector2) -> Node2D:
      assert(type >= 0 and type < len(obstacles))
      var new_obstacle : Node2D = obstacles[type].instance()
      new_obstacle.position = pos
      add_child(new_obstacle)
      return new_obstacle

Thats a warning, not an error. Ignore it, or add

#warning-ignore:unused_variable

in the line before it.

I created a ProgressionTester.tscn which is a Node2D node and added the code. It works perfectly fine. Random obstacles are being instanced. But I tried adding the same code in Enemy.tscn which is a Position2D node ; But I am getting assertion error and the obstacles are not being instanced. Can you please take a look in the project below ?

[ I have added the ProgressionTester.tscn in Tests folder and Enemy.tscn in the Enemy folder. ]
Here is the Project

Please start a new question. This has nothing todo anymore with the topic.
Have in mind, that someone who has a similar problem might find your post and gets an answer without asking ;)
But if we continue in the comments, this is getting very confusing for any future reader.
Maybe its better if you post your problem in the forum

Sorry about that; I have posted the question as a new topic.
Here is the Question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.