0 votes

Hi,

I have cobbled together a script which moves a sprite/enemy forwards, then backwards with flip. It's for a basic enemy patrol.

I've added the Position2D Nodes for start and finish markers, similar to the way I'd do it in Unity.

Note: in the screenshot it says MovingPlatform for the scene, don't worry about that, it will eventually say EnemyPatrol (just testing)

Here's the script (Sorry for the formatting issue in this forum, not sure why)

extends Node2D

var startPoint = Vector2.AXISX
var endPoint = Vector2.AXIS
X

var time = 0
var timeDirection = 1
export var moveDuration = 2

func _ready():
startPoint = $Position2DStart.position
endPoint = $Position2DEnd.position

func process(delta):
# Flip the direction of how time gets added
# This ensures it moves back to its initial position
if (time > moveDuration or time < 0):
timeDirection *= -1
$Platform/AnimatedSprite.set
flip_h(true)

if (timeDirection > 0):
    $Platform/AnimatedSprite.set_flip_h(false)

# delta is how long it takes to complete a frame.
time += delta * timeDirection
var t = time / moveDuration

self.position = lerp(startPoint, endPoint, t)

enter image description here

The problem I have is that when I instance the scene into my main scene level, if I move the instanced scene root to the location I want, when I play the example, the enemy starts from 0,0.

If I expose the Chidlren's nodes and move the Position2DStart/Position2DEnd nodes instead, the enemy starts from those locations totally fine.

Why can't I just move the whole enemy scene node to do the same thing?

I'd like to move the whole thing so that I can see where my enemy is located in the editor, not just the markers.

Godot version 3.4.4
in Engine by (78 points)

1 Answer

+1 vote

Dont use .position that is a relative position use .global_position instead. global position is the position in absolute X/Y/Z axis whereas position is relative to the nodes view of the game.

by (2,001 points)

Yep, that worked like a charm, I knew it must be something simple.

Cheers!

Here's the updated code for anyone that requires it:

extends Node2D

var startPoint = Vector2.AXIS_X
var endPoint = Vector2.AXIS_X

var time = 0
var timeDirection = 1
export var moveDuration = 2

func _ready():
    startPoint = $Position2DStart.global_position
    endPoint = $Position2DEnd.global_position

func _process(delta):

    if (time > moveDuration or time < 0):
        timeDirection *= -1
        $Platform/AnimatedSprite.set_flip_h(true)

        if (timeDirection > 0):
        $Platform/AnimatedSprite.set_flip_h(false)

    time += delta * timeDirection
    var t = time / moveDuration

    self.global_position = lerp(startPoint, endPoint, t)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.