![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | johnviggo |
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:
player.gd:
extends KinematicBody2D
onready var sprite = $Sprite
const DISTANCE_THRESHOLD: = 2.0
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(Input.is_action_just_released("left_click")):
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
return
if(Input.is_action_pressed("left_click")):
speed = max_speed
else:
if(speed > 0.0):
speed -= 3
else:
speed = 0.0
_velocity = Steering.followMouse(_velocity, global_position, target_global_position, speed)
rotation = _velocity.angle()
if(rotation_degrees >= -90 and rotation_degrees <= 90):
sprite.flip_v = false
else:
sprite.flip_v = true
_velocity = move_and_slide(_velocity)
```
steering.gd:
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
pickup.gd
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(global_position), 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
print(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
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.
jgodfrey | 2020-02-16 17:24
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?
Magso | 2020-02-16 18:36
I have now added my, probably shoddy, code.
johnviggo | 2020-02-16 19:45
Good question, Magso. I now found this example of a pickup. Did not know about parenting like this. I will look into it.
https://forum.godotengine.org/47769/player-pick-up-object
I still however would like to know why my code acts so strange
johnviggo | 2020-02-16 19:46