• 3D
  • How to revive the player?

Hello! I want to revive the player, by pressing the left-mouse button.

Here are the "dead()" and "respawn()" functions:

The "dead()" function works fine, but the "respawn()" function doesn't work at all.

How should I improve the "respawn()" function for it to work?

How is the function not working?

Are you making the Human node visible anywhere after calling respawn? In the code provided, there doesn’t appear to be anywhere to make it visible again. Same for the disabled collision shape. Another thing to check is to make sure the respawn function is being called correctly. I generally add something like print(“respawn called”) when testing. Finally, how are the positions stored? Are they in global space (world positions) or local space (relative to the player)? If they are in global space, you could try using global_transform.origin = respawn_position in the respawn_translation function and see if that helps position the character.

The first thing that would make the function work would be the actual translation of the player to one of the spawn points on the map, which I defined in the Player's script: Here I am not sure if I should defined them in the Main Scene's script or it is ok to define them in the Player's script. But, in any case, the translation does not happen at all.

After that, I want to make the player alive again, so he can run, jump or climb. In order to take into consideration the programming logic error, I put here the scheme of "_physics_process(delta)" function:

if (is_dead) == false
	aim() function
	walking() function
	flying() function
	if-else statement that manages free fall
	dead() function
else 
	respawn() function

So, regarding this function, I want the following functionalities to work: Player's translation Enable the movement

I followed your tips and upgraded the "respawn()" function with the following functionalities: Making the mesh visible again Enable the collsion shape. * printing at the console the "respawn called" text

And here I spotted an interesting thing: The player mesh appears for a few milliseconds while I am pressing the left-click mouse button, and then dissapears. It seems like the player is alive only while I am pressing the left-click. :))

Regarding the "respawn called" text at the console, it is printed every time I press the left-click button. Therefore, the function is called.

Also, regarding the positions, should I declare them in the main scene script? What would be the reference point of those coordinates? Does the 3d scene have an Origin with (0, 0, 0) coordinates?

@Tudor said: The first thing that would make the function work would be the actual translation of the player to one of the spawn points on the map, which I defined in the Player's script: Image omitted to save space Here I am not sure if I should defined them in the Main Scene's script or it is ok to define them in the Player's script. But, in any case, the translation does not happen at all.

Looking at the positions, I'm thinking they are likely in global space, so you probably want to not alter the translation, but rather set the position of the player using something like this: global_transform.origin = random_position, where random_position is one of the Vector3s in the array.

Also, and I'm not sure if this is causing the issue, but I think you need to pass an Array of Vector3s when initializing a PoolVector3Array. Alternatively, you could also use the append function in _ready:

var array_of_positions = PoolVector3Array([
	Vector3(-95.54, 2.921, 60.43),
	Vector3(-95.54, 2.921, 15.70),
	Vector3(-95.54, 2.921, -26.61),
	# etc
])

# or, using append
func _ready():
	array_of_positions.append(Vector3(-95.54, 2.921, 60.43))
array_of_positions.append(Vector3(-95.54, 2.921, 15.70))
array_of_positions.append(Vector3(-95.54, 2.921, -26.61))

As for where you define them, I don't think it matters too much. You could use an exported variable if you want to define the positiosn in the editor though, which may be helpful?

export (PoolVector3Array) var array_of_positions = PoolVector3Array()

After that, I want to make the player alive again, so he can run, jump or climb. In order to take into consideration the programming logic error, I put here the scheme of "_physics_process(delta)" function:

if (is_dead) == false
	aim() function
	walking() function
	flying() function
	if-else statement that manages free fall
	dead() function
else 
	respawn() function

So, regarding this function, I want the following functionalities to work: Player's translation Enable the movement

I followed your tips and upgraded the "respawn()" function with the following functionalities: Making the mesh visible again Enable the collsion shape. * printing at the console the "respawn called" text

And here I spotted an interesting thing: The player mesh appears for a few milliseconds while I am pressing the left-click mouse button, and then dissapears. It seems like the player is alive only while I am pressing the left-click. :))

Huh, interesting. Maybe the code you are using for left-click makes the player visible or something? Or changes the player's position?

Regarding the "respawn called" text at the console, it is printed every time I press the left-click button. Therefore, the function is called.

Also, regarding the positions, should I declare them in the main scene script? What would be the reference point of those coordinates? Does the 3d scene have an Origin with (0, 0, 0) coordinates?

It depends on how you apply them. I would recommend using global_transform.origin , which would make the positions in global/world space, which is the position relative to the origin of the scene (position (0, 0, 0)). It's often easier to think in global coordinates rather than local ones, as then if the node that the character is parented to is offset for whatever reason, it doesn't effect the respawn positions.

By passing an array of Vector3s when initializing PoolVector3Array, instead of arrays of numbers, the problem with translation was partially solved. The player was translating, but had himself as reference point. So, he was translating chaotically on the map.

Then, when I used global_transform.origin in "respawn_translation()" function, instead of translate(get_transform().basis.xform(random_position), it solved the problem with translation completely.

Now, the player is translating in the established positions.

Also, I exported the variable "array_of_positions". As you say, it can be useful in the future.

Regarding the left-click, indeed, I program med the player to respawn when I hit the left click. It was trying desperately to resurrect the player xDD

I solved the problem with the movement in an unexpected way: I added the following functionalities in the "respawn()" function: reset the health amount from 0 to 2 (max_amount) reset the healthbar

It seems that resetting the health, cancelled the "dead()" function. And if we look at the "physics_process(delta)" function, that is actually happening.

Thank you for your great explanation and a good conversation! I hope this conversation will help others as well. :)

Great! I'm glad you were able to figure it out :smile: