how do i get and print global location of an object ( spesificly on "y" axis )

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By fol

I need to make depth meter.

I can signal value to the ui on the screen

but couldn’t find a way to get global y value\ translation of a player (kinematick body )

:bust_in_silhouette: Reply From: kidscancode

2D:

print(player.global_position.y)

3D:

print(player.global_transform.origin.y)

But where do I put this one.
in the physics process
it gives me a parser error ciclick referense

fol | 2022-03-08 19:04

found it
it was

print(self.global_transform.origin.y)

fol | 2022-03-08 20:27

it is weird
I can’t assign it to variable, but can print it just fine

fol | 2022-03-08 20:31

self. does nothing, you can leave it off.

kidscancode | 2022-03-08 22:02

Can’t assign what to a variable?

kidscancode | 2022-03-08 22:02

if i state variable in the begining and tru to asighn global transform to it . error

if i state variable in the _phisics_prosses it works and i can signal out y.transform value

fol | 2022-03-08 22:21

Without showing exactly what code you’re writing and what the actual error message is, I can’t really offer any guidance about the problem.

kidscancode | 2022-03-08 23:47

extends KinematicBody
class_name Player
signal damaged(hp)
signal char_gem(gems)
signal depth_count( depth )

BEGGINING

var gem = 0
var velocity = Vector3( 0 , 0 , 0 )
var y_velocity : float
export var health = 900
const SPEED = 6

export var jump_force = 20
export var acceleration = 15
export var air_acceleration = 5
export var gravity = 0.98
export var max_terminal_velocity = 54

export(float , 0.1 , 1) var mouse_sensitivity = 0.3
export(float , -90 ,0) var min_pitch = -90
export(float , 0, 90) var max_pitch = 90

var camera_direction = Vector3()

onready var camera_pivot = $CamerPivot

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _process(delta):
if Input.is_action_just_pressed(“ui_cancel”) :
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
health_regeneration(delta)

func _input(event):

if event is InputEventMouseMotion :
	rotation_degrees.y -= event.relative.x * mouse_sensitivity
	camera_pivot.rotation_degrees.x -= event.relative.y *mouse_sensitivity
	camera_pivot.rotation_degrees.x = clamp(camera_pivot.rotation_degrees.x , min_pitch , max_pitch)

func _physics_process(delta):

#I CANT PUT THIS VAR IN THE  BEGINNING /
var depth :int = global_transform.origin.y
handle_movement(delta)
emit_signal("damaged" , health)
emit_signal("char_gem" , gem)
emit_signal("depth_count", depth)

func handle_movement(delta):
var direction = Vector3()

if Input.is_action_pressed("forward"):
	direction -= transform.basis.z
	
if Input.is_action_pressed("backward"):
	direction += transform.basis.z
	

if Input.is_action_pressed("left"):
	direction -= transform.basis.x
	
if Input.is_action_pressed("right"):
	direction += transform.basis.x
	
direction = direction.normalized()
var accel = acceleration if is_on_floor() else air_acceleration
velocity = velocity.linear_interpolate(direction * SPEED , accel * delta)

if is_on_floor() : 
	y_velocity = - 0.01
else :
	y_velocity = clamp(y_velocity - gravity , -max_terminal_velocity , max_terminal_velocity)

if Input.is_action_pressed("break") and is_on_floor() :
	y_velocity = jump_force
	
velocity.y = y_velocity
velocity = move_and_slide(velocity , Vector3.UP)

func health_regeneration(delta):
if health <= 999 and health >= 1 :
health += 10 *delta

func _on_GEM_gem_collected():
gem += 1

fol | 2022-03-09 10:37

There is a button to format your code correctly when posting it, btw. It looks like {}.

You still didn’t say what error you received, but if I understand you correctly, you’re saying you want to put the line

var depth :int = global_transform.origin.y

At the beginning of your code, where you declare your variables. There are several problems with that:

  1. There is no global position before the node is in the scene tree. Global position is derived from the node’s parent.

  2. If you set the value of your depth variable when the script is initialized, it will never change, so when your object moved, that value would remain unchanged.

I’m not sure why you want to emit your global y position every single frame, but if that’s what you want, then that variable isn’t really serving any purpose anyway.

emit_signal("depth_count", global_transform.origin.y)

would do the same thing.

kidscancode | 2022-03-09 15:13

Ow.

Got it. I wanted to make a depth counter to show the progression of a character .
It is working now, but I don’t know how the engine handles large distances.

For example. My character falls and dodges ruble on the way down. (and there is a counter for that)))

Is it okay if I let it fall to 99 999 999 mil units? And the ruble despawns of course.

fol | 2022-03-09 16:27