Player starts at world origin, regardless of where they're placed in the scene

Godot Version

4.2.1

I’m having a bit of an issue right now that I could use some advice on. I’ve got a CharacterBody3D player instance in my level scene, and ideally I’d like it to start where I place it, but it always starts at 0,0,0 whenever the game is run. I can go into the player scene and modify the position there, and the changes will take effect when I run the game, but moving the instance itself has no effect. There’s nothing in my code that directly sets the player’s position, so I’m really not sure why this is happening. I’ve got other instances of scenes in my level that don’t have this behavior, it seems to only affect the player. Is there some quirk to CharacterBody3D that I’m missing?

Additionally, I tested the player in a totally new level as well, just to rule out the possibility of something in the scene being the culprit, but I still encountered the same issue.

Here’s the code I’m using for my character movement:

	//Get our input direction, and handle movement/deceleration
		Vector2 inputDirection = Input.GetVector("Left", "Right", "Forward", "Back");
		Vector3 targetDirection = (Transform.Basis * new Vector3(inputDirection.X, 0, inputDirection.Y)).Normalized();
		direction = direction.Lerp(targetDirection, moveLerpSpeed * (float)delta);

		if (direction != Vector3.Zero)
		{
			newVelocity.X = direction.X * currentSpeed;
			newVelocity.Z = direction.Z * currentSpeed;
		}
		else
		{
			newVelocity.X = Mathf.MoveToward(newVelocity.X, 0, currentSpeed);
			newVelocity.Z = Mathf.MoveToward(newVelocity.Z, 0, currentSpeed);
		}
		Velocity = newVelocity;

		MoveAndSlide();

It would be very useful to have more information to help you. Could you share your project so we can reproduce this error and take a deeper look?

Oh sure! I’ll put it up on my Google Drive

I’d definitely appreciate someone going and having a peek at it. I even control+f’d through all of my code to see if the player’s position was specifically set somewhere, but I couldn’t find it.

Your problem is the line 31 in PlayerMovement.cs:

Transform3D transform;

Delete that, and your problem will be fixed. That line is restarting your transform. Always avoid using the names of the predefined variables provided by the class. So avoid creating variables called transform, position, rotation, etc.

1 Like

Oh brilliant! Not sure why I had that there, but it definitely was the culprit. Thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.