Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | seamuskills |
I’m trying to create projectiles, and use the same scene for enemies and the player alike. However obviously the projectiles that the player shoots and the ones the enemy shoots shouldn’t have the same target. I have a variable in the projectile called target meant to be a Vector2, in the _ready function I subtract global_position from target. This is a problem because I can’t figure out how the player can set the target to the mouse position. The solution I tried was this:
if (Input.is_action_pressed("fire")):
var proj = projectile.instance()
proj.global_position = body.global_position
proj.target = get_viewport().get_mouse_position()
get_parent().add_child(proj)
var projColor = get_node(str(proj.get_path())+"/body/projectile")
projColor.color = get_node(str(body.get_path())+"/ColorRect").color
the problem is I keep getting this error: “Invalid set index ‘target’ (on base: ‘Node2D’) with value of type ‘Vector2’.”
I cant find any solution that cover my specific scenario, thanks for reading, and have a nice day!
What does the script of the projectile look like?
With this script on the root node of the scene
# Main.gd
extends Node
const enemy_scene = preload("res://Enemy.tscn")
onready var my_char = $Player
onready var monster = $Enemy
func _ready():
my_char.target = monster
monster.target = my_char
print("MyChar target to " + my_char.target.name)
print("Monster target to " + monster.target.name)
func _unhandled_input(event):
if event is InputEventKey:
if event.is_action_pressed("ui_accept"):
var enemy = enemy_scene.instance()
enemy.print_my_vec2()
enemy.my_vec2 = Vector2(-4, 8)
enemy.print_my_vec2()
I can manipulate the vec2 of the instanced node as you do.
the output looks like following:
MyChar target to Enemy
Monster target to Player
Enemy vec2: (0, 0)
Enemy vec2: (-4, 8)
the other script:
# ToTarget.gd
extends Node2D
var target setget set_target, get_target
var my_vec2: = Vector2()
func set_target(t)->void:
target = t
func get_target():
return target
func print_my_vec2():
print(name + " vec2: " + str(my_vec2))
ener | 2021-12-30 09:28
I don’t understand any of that, I’ve used godot for like a day so my code isn’t that organized :\
seamuskills | 2022-01-05 18:16