简体   繁体   中英

I have a question about a YT tutorial because I wanna customize it a little

in this video, https://youtu.be/klBvssJE5Qg I shows you how to spawn enemies outside of a fixed camera. (this is in GDscript by the way) How could I make this work with a moving camera? I wanna make a zombie fighting game with a moving camera and zombies spawning outside that. I would really appreciate help with this.

 I've tried researching on the internet about how to do it, but I just didn't find it.

N/A..................................

After looking at the video, I see they are using this line to spawn:

Global.instance_node(enemy_1, enemy_position, self)

This suggest to me a couple thing:

  • The position is probably either relative to the self passed as argument or global.
  • There must be an Autoload called Global that I need to check to make sure.

And the answer is in another castle video.

In the video Godot Wave Shooter Tutorial #2 - Player Shooting we find this code:

extends Node

func instance_node(node, location, parent):
    var node_isntance = node.instance()
    parent.add_child(node_instance)
    node_instance.global_position = location
    return node_instance

And thus, we are working with global coordinates global_position . Thus enemy_position is used as global coordinates.

Ok, instead of using enemy_position as global coordinates we are going to use it as local coordinates of the Camera2D (or a child of it). Which means you need a reference to the Camera2D (which I don't know where do you have it).

You could make your code in a child of the Camera2D , or take the transform of the Camera2D using a RemoteTransform2D . Either way, you could then work in its local coordinates. Thus you would do this:

Global.instance_node(enemy_1, to_global(enemy_position), self)

Or you could have a reference by exporting a NodePath (or in the newest Godot you can export a Camera2D ) from your script and set it via the inspector. So you can do this:

Global.instance_node(enemy_1, camera.to_global(enemy_position), self)

Where camera is your reference to the Camera2D .

In the following section of Arena.gd:

func _on_Enemy_spawn_timer_timeout():
    var enemy_position = Vector2(rand_range(-160, 670), rand_range(-90, 390))

I believe you can add the X and Y coordinates of the camera to their corresponding random ranges in the enemy position Vector2. This will displace the enemy depending on where the camera is currently located.

You can get the position of the camera with this:

get_parent().get_node("Name of your camera").position

When this is all put together:

func _on_Enemy_spawn_timer_timeout():
    var enemy_position = Vector2(rand_range(-160, 670) + get_parent().get_node("Name of your camera").position.x, rand_range(-90, 390) + get_parent().get_node("Name of your camera").position.y)

Keep in mind that you might need to displace the values in the following while loop as well. I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM