As the title says, I want to keep the player still whenever I disable the movement. To disable this, I use a variable "movement", which only lets the player move whenever it is true. The issue is that whenever I set this variable to false, if the player is moving, it will keep the player's momentum, that is, the player keeps moving in the same direction it was going just before stopping the movement.

I would appreciate any help (using Godot 3.5 btw)

  • xyz replied to this.

    Yeah sorry for that, here it is:

    if GlobalScript.movement == true:
    	#MOVEMENT
    		direction = Vector3.ZERO
    		var h_rot = global_transform.basis.get_euler().y
    		var f_input = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
    		var h_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
    		direction = Vector3(h_input, 0, f_input).rotated(Vector3.UP, h_rot).normalized()
    	#JUMP AND GRAVITY
    		if is_on_floor():
    			snap = -get_floor_normal()
    			accel = 7
    			gravity_vec = Vector3.ZERO
    		else:
    			StopRunningAudio()
    			snap = Vector3.DOWN
    			accel = 1
    			gravity_vec += Vector3.DOWN * gravity * delta
    			
    		if Input.is_action_just_pressed("jump") and is_on_floor():
    			snap = Vector3.ZERO
    			gravity_vec = Vector3.UP * jump
    		
    		#make it move
    		velocity = velocity.linear_interpolate(direction * speed, accel * delta)
    		movement = velocity + gravity_vec
    	
    	move_and_slide_with_snap(movement, snap, Vector3.UP)
    • xyz replied to this.

      Darxkl05 Where do you test if movement is true. Looks like it doesn't even have a boolean value.

        xyz All of the previous code is inside an if statement which checks if the variable movement is true. I just edited the code from before, sorry for the confusion

        • xyz replied to this.

          Darxkl05 Post the complete _process() and _physics_process() functions code.

          Thanks for the answers but I managed to eventually solve it.

          To disable the movement without keeping the momentum, since this was being called from another script, I just had to do this:

          $Player.movement = Vector3.ZERO
          GlobalScript.movement = false

          At first I tried setting velocity to Vector3.ZERO but it didn't work out. However, setting vector3.Zero to movement completely stops the player, and then it completely disables the movement by setting the GlobalScript variable to false