this is my code

extends CharacterBody3D

const SPEED = 5.0
const SPRINT_SPEED = 10.0  # Adjust as needed
const CROUCH_SPEED = 2.0   # Adjust as needed
const JUMP_VELOCITY = 4.5  # Increase the jump velocity for a more noticeable effect

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

# Crouch variables
var is_crouching = false
var original_scale = Vector3(1, 1, 1)
var crouched_scale = Vector3(1, 0.5, 1)

func _ready():
	original_scale = scale

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jump.
	if is_on_floor() and Input.is_action_just_pressed("jump"):
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

	if direction:
		# Sprinting
		if Input.is_action_pressed("sprinting"):
			velocity.x = direction.x * SPRINT_SPEED
			velocity.z = direction.z * SPRINT_SPEED
		# Regular movement
		else:
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED
	else:
		velocity.x = 0
		velocity.z = 0

	# Crouch
	if Input.is_action_pressed("crouching"):
		crouch()
	else:
		stand()

	move_and_slide()  # Apply velocity without arguments

func crouch():
	if not is_crouching:
		is_crouching = true
		scale = crouched_scale
		position.y -= 1.0  # Adjust the position to maintain the same eye level

func stand():
	if is_crouching:
		is_crouching = false
		scale = original_scale
		position.y += 1.0  # Adjust the position when standing up

idk why but my code breaks when i post it

  • xyz replied to this.

    Elroy Enclose it in block code tags:
    ~~~
    code
    ~~~

    thanks now it show correct

    • xyz replied to this.

      Elroy You should also specify in what way the code isn't working.

        xyz

        Well the movement doesnt work i can go forward backwars or anything i tried rebinding it to azerty movment but it doesnt do anything

        • xyz replied to this.

          Elroy Try printing the velocity value just before calling move_and_slide()

          extends CharacterBody3D
          
          const SPEED = 5.0
          const SPRINT_SPEED = 10.0  # Adjust as needed
          const CROUCH_SPEED = 2.0   # Adjust as needed
          const JUMP_VELOCITY = 4.5  # Increase the jump velocity for a more noticeable effect
          
          # Get the gravity from the project settings to be synced with RigidBody nodes.
          var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
          
          # Crouch variables
          var is_crouching = false
          var original_scale = Vector3(1, 1, 1)
          var crouched_scale = Vector3(1, 0.5, 1)
          
          func _ready():
          	original_scale = scale
          
          func _physics_process(delta):
          	# Add the gravity.
          	if not is_on_floor():
          		velocity.y -= gravity * delta
          
          	# Handle jump.
          	if is_on_floor() and Input.is_action_just_pressed("jump"):
          		velocity.y = JUMP_VELOCITY
          
          	# Get the input direction and handle the movement/deceleration.
          	# As good practice, you should replace UI actions with custom gameplay actions.
          	var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
          	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
          
          	if direction:
          		# Sprinting
          		if Input.is_action_pressed("sprinting"):
          			velocity.x = direction.x * SPRINT_SPEED
          			velocity.z = direction.z * SPRINT_SPEED
          		# Regular movement
          		else:
          			velocity.x = direction.x * SPEED
          			velocity.z = direction.z * SPEED
          	else:
          		velocity.x = 0
          		velocity.z = 0
          
          	# Crouch
          	if Input.is_action_pressed("crouching"):
          		crouch()
          	else:
          		stand()
          
          	print("Velocity:", velocity)  # Print the velocity before moving
          	move_and_slide()  # Apply velocity without arguments
          
          func crouch():
          	if not is_crouching:
          		is_crouching = true
          		scale = crouched_scale
          		position.y -= 1.0  # Adjust the position to maintain the same eye level
          
          func stand():
          	if is_crouching:
          		is_crouching = false
          		scale = original_scale
          		position.y += 1.0  # Adjust the position when standing up

          i did this no movement and got this message 870 times Velocity: (0, 0, 0)

          • xyz replied to this.

            Elroy Now do the same for input_dir to see if the input actions are getting picked up.

            i got these 2 outputs Input
            Direction: ( 0, 0)
            Velocity: (0, 0, 0)

            extends CharacterBody3D
            
            const SPEED = 5.0
            const SPRINT_SPEED = 10.0  # Adjust as needed
            const CROUCH_SPEED = 2.0   # Adjust as needed
            const JUMP_VELOCITY = 4.5  # Increase the jump velocity for a more noticeable effect
            
            # Get the gravity from the project settings to be synced with RigidBody nodes.
            var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
            
            # Crouch variables
            var is_crouching = false
            var original_scale = Vector3(1, 1, 1)
            var crouched_scale = Vector3(1, 0.5, 1)
            
            func _ready():
            	original_scale = scale
            
            func _physics_process(delta):
            	# Add the gravity.
            	if not is_on_floor():
            		velocity.y -= gravity * delta
            
            	# Handle jump.
            	if is_on_floor() and Input.is_action_just_pressed("jump"):
            		velocity.y = JUMP_VELOCITY
            
            	# Get the input direction and handle the movement/deceleration.
            	# As good practice, you should replace UI actions with custom gameplay actions.
            	var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
            	print("Input Direction:", input_dir)
            
            	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
            
            	if direction:
            		# Sprinting
            		if Input.is_action_pressed("sprinting"):
            			velocity.x = direction.x * SPRINT_SPEED
            			velocity.z = direction.z * SPRINT_SPEED
            		# Regular movement
            		else:
            			velocity.x = direction.x * SPEED
            			velocity.z = direction.z * SPEED
            	else:
            		velocity.x = 0
            		velocity.z = 0
            
            	print("Velocity:", velocity)  # Print the velocity before moving
            
            	# Crouch
            	if Input.is_action_pressed("crouching"):
            		crouch()
            	else:
            		stand()
            
            	move_and_slide()  # Apply velocity without arguments
            
            func crouch():
            	if not is_crouching:
            		is_crouching = true
            		scale = crouched_scale
            		position.y -= 1.0  # Adjust the position to maintain the same eye level
            
            func stand():
            	if is_crouching:
            		is_crouching = false
            		scale = original_scale
            		position.y += 1.0  # Adjust the position when standing up

            and still doesnt work

            • xyz replied to this.

              Elroy and still doesnt work

              Of course it doesn't but now you have a clue about why it isn't working. If input_dir is (0,0) this means that Input.get_vector() doesn't return a proper direction vector when you press your commands. Check if action names you use as arguments when calling Input.get_vector() are actually defined in project settings.

              okay but how do i check that ?

              • xyz replied to this.

                Elroy Where did you get those action names from in the first place?

                You can check/map input actions in Project > Project Settings > Input Map

                For a quick start you can try using default actions for cursor keys:

                var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")

                i got them from a video on youtube that changes the movement from the arrow keys to the azerty keys

                oke i tried the arrow keys and that does work


                these are the inputs i use

                • xyz replied to this.

                  Elroy Looks like you don't have any actual keys mapped to those actions. Click the plus icon for each and assign a key.

                  ooooh okay thank you

                  thank you its working now, i feel stupid xp but thanks, only thing is when i go crouch and let it go , its like i get puched in the air a little and then back to the ground

                  • xyz replied to this.