The player ignores local positioning

I’m working on a movement script in C# for my game. Everything seems set up correctly, with local variables instead of global ones. However, my player doesn’t move in the expected direction relative to its rotation. For example, when I turn around and press the forward key, the player moves backward. It seems like the player is using global position instead of local position and rotation, even though I’ve confirmed it’s set to use local position and rotation.

using Godot;
using System;

public partial class gumgum_movment : CharacterBody3D
{
	public float posX= 0;
	public float posY=0;
	public float posZ=0;
	public float gravity=-30;
	public float acc_posY=-30;
	float acc_posX=0;
	float acc_posZ=0;
	

	public bool abletojump=false;
	public bool jump_rest=false;

	public float velocity_Y=0;

	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{     
		
       walking_handling(delta);

         jumping_handling(delta);


	   gravity_handling(delta);

	  
	
	

	}




public override void _PhysicsProcess(double delta)
{
	
	
}




	public void walking_handling(double delta2)
	{


         if(Input.IsKeyPressed(Key.A))
		{
		   posX-=20*(float)delta2;
		}else  if(Input.IsKeyPressed(Key.D))
		{
			 posX+=20*(float)delta2;
		}


		
		

		

		if(Input.IsKeyPressed(Key.W))
		{
			 posZ-=20*(float)delta2;
		}else if(Input.IsKeyPressed(Key.S))
		{
			 posZ+=20*(float)delta2;
		}


        Position=  new Vector3(posX,posY,posZ);
		 
	}





   public void gravity_handling (double delta3)
   
   {
			
		//applying acc
		
		velocity_Y+=acc_posY*(float)delta3;
		
		
		//applying velocity

		
		posY+=velocity_Y*(float)delta3;

		if(!abletojump)
		{
			acc_posY=gravity;
		}
		

   }


   public void jumping_handling(double delta2)
   {
	

		if(Input.IsActionJustPressed("jumping"))
		{
			GD.Print("jumping");
			acc_posY=60;
			
		}



   }
   
  




}

Two things:

First, if you wrap your code block in ``` it will render as code here, which will make it much more readable.

Second, I would confirm that the local position is what you think it is, given that local is related to the transforms of the parents. Like, ignoring the code, if you go into the editor, and in the remote scene when the code is running and find your node, and adjust the Node3D transform position of your player, increasing the X by some amount, does he move to the right like you expect? If so, then there may be something wrong with the code somehow, but if not, then the problem is in the orientation of your scene hierarchy instead.

Your code does not make use of the player’s angle so there is no way they would be able to walk “forward”. You will have to make use of Basis.Z to face the movement vector the correct way, but you also avoid creating a traditional movement vector. Set up actions in the InputMap in project settings and use Input.GetVector instead of directly checking keys in if statements.

An attached gdscript CharacterBody3D has a movement template I would recommend looking at.

i tried in both the local and remote positioning like tinkering with the values and both of them have the same problem

Move in local X,Y,Z and global X,Y,Z have direction of XYZ.
Adding x to global and local position will be the same.
Maybe your camera is a problem :wink:

but i used the local positioning of the node it should work or?

Consider if this did work, walking forward on the child results in a Z offset, now rotating the parent swings the child around the rotation axis with a Z offset.

Rotating a node does not make it’s position rotated, further position changes are still axis-aligned to the parent.

1 Like