This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 )

Godot version 3.2.3
in Engine by (12 points)

1 Answer

0 votes

2D:

print(player.global_position.y)

3D:

print(player.global_transform.origin.y)
by (22,191 points)

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

found it
it was

print(self.global_transform.origin.y)

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

self. does nothing, you can leave it off.

Can't assign what to a variable?

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

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

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.

extends KinematicBody
classname 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 jumpforce = 20
export var acceleration = 15
export var air
acceleration = 5
export var gravity = 0.98
export var maxterminalvelocity = 54

export(float , 0.1 , 1) var mousesensitivity = 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
mousemode(Input.MOUSEMODE_CAPTURED)

func process(delta):
if Input.is
actionjustpressed("uicancel") :
Input.set
mousemode(Input.MOUSEMODEVISIBLE)
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 physicsprocess(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 onGEMgemcollected():
gem += 1

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.

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.

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.