The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

I was making a 2D game where the player has to run from a boulder that is chasing them. But whenever the player jumps the boulder also moves up so I need to only get the X coordinates of the player so that it will stay on the ground. This is the boulder code:

    extends KinematicBody2D


var nearest_player
var velocity = Vector2()
var collide

export var gravity = 9000000000000000

export var health = 5
export var speed = 1.0
export var damage = 1
export var wait_time = 0.1
var tile_size = 10

var isMove = true
var isAttack = false

func _ready():

    get_target()

func get_target():
    var players = []

    players += get_tree().get_nodes_in_group("player")

    if players.size() > 0:
        nearest_player = players[0]

    for player in players:
        if player.global_position.distance_to(global_position) < nearest_player.global_position.distance_to(global_position):
            nearest_player = player

func _process(delta):
    if speed != 500.0:
        speed += 0.1
    velocity.x = 0
    velocity.y += gravity
    if isMove and is_instance_valid(nearest_player):
        velocity = (nearest_player - global_position).normalized() * speed

        collide = move_and_slide(velocity)

    for i in get_slide_count():
        var collision = get_slide_collision(i)
        var collider = collision.collider
        if is_instance_valid(collider) and collider.is_in_group("player"):
            get_tree().reload_current_scene()

    if health <= 0:
        call_deferred("free")

func enemy_hit(dmg):
    health -= dmg
Godot version 3.4.2
in Engine by (93 points)

1 Answer

+1 vote

You can do something like this

var vec = Vector2()

func find_player():
    vec = get_node("Root/tothe/PlayerSprite").position
    print(vec.x)
by (3,328 points)
edited by
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.