Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Scavex |
Hey I guys I am trying to create a Canon which will always point and shoot towards my player and the canon is successfully always facing him but when I tried this script :
extends Sprite
var player=null
var rot_sped=0.1
var fire_rate=0.5
var fire_time=0.0
var canonbullet=preload("res://CanonBullet.tscn")
func _physics_process(delta):
if player==null:
return
var dir_to_player=player.global_position-global_position
rotation=dir_to_player.angle()
fire_time+=delta
if fire_time>fire_rate:
fire_time=0
fire()
func fire():
print("fire")
var canonbullet_inst=canonbullet.instance()
get_tree().get_root().add_child(canonbullet_inst)
canonbullet_inst.global_position=global_position
var dir_to_player=(player.global_position-global_position).normalized()
canonbullet_inst.move_dir=dir_to_player
func set_player(p):
player=p
I’m getting Invalid set index ‘move_dir’ (on base: ‘KinematicBody2D’) with value of type ‘Vector2’ on
canonbullet_inst.move_dir=dir_to_player
This here is my CanonBullet Script :
extends KinematicBody2D
var move_dir=Vector2()
var move_speed=80
func _physics_process(delta):
var coll=move_and_collide(move_dir*move_speed*delta)
if coll:
if coll.collider.name=="Player":
print("You Died")
queue_free()
I fixed it ! My CanonBullet Script wasn’t attached
Scavex | 2020-04-23 14:37
So urm… how did you attach it? Same problem here.
BuddyGames | 2021-01-31 22:17
I just realized that even though I had this script, it wasn’t attached to Cannon Bullet. So I simply just went tot he Cannon Bullet scene. After that I just selected the top node in that scene, did the same step that’ll you do to add a script but instead you will have an option of selecting the script you want to add and that was it !
Scavex | 2021-02-01 09:30