@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.