0 votes

I have a strange issue i do not understand how to get around.

I control the player by holding down the left mouse button and move the mouse around. The player follows the mouse.

I have a pickup that when it is hit, i update its global position to be the same as the players global_position in the pickups _process-function. I clearly see it works because i have stopped turning it invisible and it follows the center of the player.

When I stopp pressing the left mouse button, the pickup is suposed to be spit out from the player in the position the player (and the pickup) is in at that moment. That works like a charm if i have print(global_position) in my _process-function in my pickup.gd.

If i comment that printing out, the moment I stop pressing the left mouse button, the pickup snaps from the center of the player an back to around where i picked it up and it starts falling from there.

What am I missing? I am rally new to this so the way I have tried solving what i want might be idiotic. I just do not understand why it behaves so differentli with and witheout one line with print! Here is my code:


extends KinematicBody2D

onready var sprite = $Sprite
export var max_speed = 100.0
var speed: = 0.0
var _velocity: = Vector2.ZERO
var consumed: RigidBody2D = null

func _physics_process(delta):
    var target_global_position: Vector2 = get_global_mouse_position()

        if consumed != null:            
            consumed.global_position = global_position
            consumed.IsConsumed = false
            consumed.UseGravity = true
            consumed.visible = true
            consumed.linear_velocity *= 0
            consumed.angular_velocity *= 0
            consumed = null
    if(global_position.distance_to(target_global_position) < DISTANCE_THRESHOLD):
        speed = 0.0
        _velocity = Vector2.ZERO
        speed = max_speed
        if(speed > 0.0): 
            speed -= 3
            speed = 0.0
    _velocity = Steering.followMouse(_velocity, global_position, target_global_position, speed) 
    rotation = _velocity.angle()
    if(rotation_degrees >= -90 and rotation_degrees 


extends Node

const DEFAULT_MASS = 2.0
const DEFAULT_MAX_SPEED = 200.0

static func followMouse(
        velocity: Vector2,
        global_position: Vector2,
        target_position: Vector2,
        max_speed: = DEFAULT_MAX_SPEED,
        mass: = DEFAULT_MASS
) -> Vector2:
    var desired_velocity = (target_position - global_position).normalized() * max_speed
    var steering: Vector2 = (desired_velocity - velocity) / mass
    return velocity + steering


In here lies the «magic». If i have the process like this it works OK. If all i do is stop the game, comment out print(globalposition), and start the game, it behaves crazy again.

extends RigidBody2D

var IsConsumed: = false
var UseGravity: = false
onready var player: = get_tree().root.get_node("World").get_node("Player")

func _process(_delta):
    if IsConsumed == true:
        global_position = player.global_position

func _physics_process(delta):           
    if UseGravity == true:
        linear_damp = 0
        angular_damp = 2
        linear_velocity += Vector2(0, 200) * delta

func _on_Pickup_body_entered(body):
    if body.name == "Player" and !IsConsumed and player.consumed == null:
        visible = false
        player.consumed = self
        IsConsumed = true
func _on_Pickup_body_shape_exited(body_id, body, body_shape, local_shape):
    if body.name == "Player" and IsConsumed == false:
        UseGravity = true
        visible = true
in Engine by (12 points)
edited by

You probably need to post the relevant code because, as you suspect, a print statement shouldn't have any impact on the rest of the code. I assume the problem is actually something different.

How does the pickup follow the player? Is it being parented back and forth between the player and scene or is it using an offset that ends up resetting once the mouse is released?

I have now added my, probably shoddy, code.

Good question, Magso. I now found this example of a pickup. Did not know about parenting like this. I will look into it.


I still however would like to know why my code acts so strange :)

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.