How is gd not reading this?!

Godot Version

Godot 4.2.2

Question

What am I doing wrong here? Im not familiar with gd script, but this seems fairly simple, and self_explanatory. Its basically a bullet script. I dont understand why it’s not reading anything about the ‘dir’ variable. I checked in remote view, and it IS reading when he goes from 1 to -1 in the xscale, so then how is this chunk of code not reading the ‘dir’ variable? Oh, and using flip_h doesnt work either. So, am I missing something, is there another method of determining the direction something should go in I havent tried?

extends Area2D
@onready var FireshotSprite = $FireshotSprite
@onready var RedmageSprite = $“…”
@onready var dir = RedmageSprite.scale.x
@export var speed = 100

func _ready():
set_as_top_level(true)

func _physics_process(delta):
position.x += dir * speed * delta
if dir < 0:
FireshotSprite.scale.x = -1

func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()

You need to update dir as well, it won’t be linked to the scale, you need to update it yourself manually, by dir = ...

But you also can’t use negative x scales, see here

1 Like

I see. Thank you, that explains why its not reading it. That’s too bad, I cant think of another way to determine the direction…

You can just use h_flip or something similar, but you can check the value just use dir = FireshotSprite.scale.x or similar, but it won’t update itself

Is there a benefit to using scale to move the position as opposed to say, dir = Vector2(1,0)?

As far as I understand scale is isn’t a property you would want to use for this.

Were you just trying to use scale to tell the fireball to move right vs left?

In my current project I have a “muzzle” or marker node on my character that rotates with the character and acts as a point of origin for projectiles. Bullets spawn, rotated, at that point and use the dir variable as a vector 2. Without the rotation they would just travel to the right but by rotating it with the muzzle they shoot in the desired direction.

2 Likes

yeah, that’s what I was trying to use it for. It works in game maker… Ill have to figure something else out :confused: Im using the gamepad for input, otherwise the mouse would make the rotation feature fairly simple. I was wondering what a thunbstick rotation would look like, but then I decided I was just going to go with a left or right thing for my game. like megaman…

1 Like

Oh okay, that makes more sense as to the why. Every day we learn something new!

I think you’re on the right track, love me some Mega Mann!

If you try the muzzle approach, it might work with some code that either rotates it 180 when you walk left. That’s code on your player though, not the bullet.

Having code for your bullets movement so it’s in one direction, then manipulating that direction in your player script should do the trick.

There’s gotta be a Mega man in godot tutorial somewhere on YouTube, maybe skip to the shooting section for some ideas.

1 Like

I got it working, finally:

extends Area2D
@onready var FireshotSprite = $FireshotSprite
@export var speed = 100
@onready var RedmageSprite = $“…/Redmage/RedmageSprite”
@onready var direction = Vector2(RedmageSprite.scale.x, RedmageSprite.scale.y)

func _ready():
set_as_top_level(true)

func _physics_process(delta):
position.x += direction.x * speed * delta
if direction.x < 0:
FireshotSprite.scale.x = -1

func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.