S
SilverWolvesGames

  • Dec 27, 2023
  • Joined Dec 3, 2023
  • 0 best answers
  • Hi everyone.
    How can I implement in Godot a custom camera confiner 2D (similar to what we have in Unity) ?
    Here is an example of what i'm talking about :

    (Look at the video from 7:48 to 10:30)

  • Yeah exactly ! And I haven't noticed any changes in the behaviour of my game.

  • I've found the problem. In the editor settings, i've checked a checkbox called "save on focus lost", and after googling a lot, i've found the issue. So i've nchecked it and now everything is fine.
    What is worrying though is that this problem is still persisting (it has been discovered in Godot 4.0)

    • Does anyone know why this error message is ?

      Parent node is busy setting up children, add_child() failed. Consider using add_child.call_deferred(child) instead.
      scene/main/window.cpp:1658 - Condition "!is_inside_tree()" is true.

      • Toxe replied to this.
      • FreakyGoose

        I have finally achieved completing swimming movement :

        class_name WaterState
        
        extends PlayerMovementState
        
        var input_direction := Vector2.ZERO
        var is_swimming_fast := false
        var exit_water := false
        
        func enter():
        	if player.facing_direction == -1:
        		player.flip_controller(1)
        		player.sprite.scale.x = -1
        	elif player.facing_direction == 1:
        		player.flip_controller(1)
        		player.sprite.scale.x = 1
        		
        
        func update(_delta):
        	if player.is_on_surface && (INPUT.JUMP || is_swimming_fast):
        		switch_state("JumpPlayerState")
        		
        	set_swim_facing_direction(player.swim_facing_dir)
        	
        	
        func physics_update(delta):
        	set_anims()
        	player.handle_surface(delta)
        	
        	set_input_normalized(delta)	
        	
        		
        func exit():
        	player.sprite.rotation = 0.0
        	player.sprite.scale.x = 1
        	is_swimming_fast = false
        	exit_water = true
        
        
        func set_swim_facing_direction(input: float) -> void:
        	if input > 0 && player.velocity.x < 0:
        		player.swim_facing_dir *= -1
        	elif input < 0 && player.velocity.x > 0:
        		player.swim_facing_dir *= -1
        
        
        func set_anims():
        	if (player.is_on_surface || player.can_jump_out_of_water()) && INPUT.X == 0:
        		play_anim("swim_surf_idle")
        	elif (player.is_on_surface || player.can_jump_out_of_water()) && INPUT.X != 0:
        		play_anim("swim_surf")
        	elif player.is_in_water() && INPUT.X == 0:
        		play_anim("swim_idle")
        	elif player.is_in_water() && INPUT.X != 0:
        		play_anim("swim")
        
        
        func set_input_normalized(delta : float):
        	input_direction = Vector2(Input.get_axis("run_l", "run_r"), Input.get_axis("climb", "crouch")).normalized()	
        	
        	if input_direction.x > 0:
        		input_direction.x = 1
        	elif input_direction.x < 0:
        		input_direction.x = -1
        		
        	if input_direction.length() > 0:
        		var angle = input_direction.angle() + deg_to_rad(90.0)
        		if !(player.is_on_surface || player.can_jump_out_of_water()):
        			player.sprite.rotation = lerp_angle(player.sprite.rotation, angle, 0.1)
        			
        			if input_direction.length_squared() >= 1 and !is_input_direction_equal_agent_direction(input_direction.x):
        				player.sprite.scale.x = player.swim_facing_dir
        		else:
        			player.sprite.scale.x = player.swim_facing_dir
        			player.sprite.rotation = lerp_angle(player.sprite.rotation, 0.0, 0.4)
        
        		set_swim_velocity(delta)	
        	else:
        		player.velocity = lerp(Vector2.ZERO, player.velocity, pow(2, -player.data.swim_control * delta))
        		
        	
        func set_swim_velocity(delta : float):
        	if player.is_on_surface || player.can_jump_out_of_water():
        		player.apply_horizontal_velocity(player.data.swim_speed, delta)
        		if INPUT.Y >= 0:
        			player.reset_vertical_velocity()
        		elif INPUT.Y < 0:
        			player.velocity = lerp(player.velocity, input_direction * Data.swim_speed, Data.swim_accel * delta)
        	elif player.is_in_water():
        		if INPUT.SPRINT:
        			is_swimming_fast = true
        			player.velocity = lerp(player.velocity, input_direction * Data.swim_speed * 2, Data.swim_accel * delta)
        		elif !INPUT.SPRINT || INPUT.SPRINT_END:
        			is_swimming_fast = false
        			player.velocity = lerp(player.velocity, input_direction * Data.swim_speed, Data.swim_accel * delta)
        	
        		
        func is_input_direction_equal_agent_direction(x_input_dir) -> bool:
        	##Check if player is currently facing input direction
        	if x_input_dir == player.sprite.scale.x:
        		return true
        	else:
        		return false
      • Hello everyone. i'm currently working on 2d platformer game in which i've implemented a hierarchical state machine.
        To handle my animations, i'm using an animation player combined with a animation tree.
        As my animation tree gets more complex, i want to group my animations in state machines.

        For instance, one state mahine for ground animations, one state machines for in air anims, one state machine for wall anims, etc...
        I need it to create concurrent state machines, very useful for implementing power-ups animations
        My issue though is that i do not know how can i exit from a currently playing anim, then the state machine in which the anim is through the code, enter a new one and play the animations inside

        The architecture of my state machine in the scene is that all of my state nodes are child of a unique state machine node, on which a state machine script is attached to.

        Can someone help me please ?

      • Thanks ! As a matter of fact, I've found a way to bypass mi issue, so this post is not useful anymore.
        Thank you for your attention !

      • I'm trying to implement a sub state machine in my animation tree. I want to trigger the animation inside, but it is always failing







        Can you help me please ?
        Also, is my code good or not ?

      • FreakyGoose
        I've currently imlemented the code you've shown and it works quite well, but i've an issue.
        When i'm swimming upward or downward, without applying any horizontal input the sprite disappears. Can you help me please ?

        • I also would like to make a multiplayer fps shooter, with simplicity in mind, but with a solid, rugged, and dynamic gameplay, with a huge consideration to environments, in a very low-poly style.

          • My dream game would be an aviation combat game, with very strong focus on characters and their stories like Top Gun Maverick, high-fidelity but reliable airplane physics and intense and more realistics dogfights, in an 3d open world environment.
            My goal is to make an interesting game, captivating the player, and not boring like Ace combat 7 in some aspects

          • Hi everyone ! I'm currently working on a 2d game and i would like to add a swimming logic similar to the one found in Ori and the will of the wisps. But i encounter big issues about flipping the character in water and orient the player upward while swimming in opposite direction. I've recorded what i want to achieve :

            ori.zip
            2MB

            Can you help me please ?

            • Toxe and FreakyGoose replied to this.
            • SilverWolvesGames likes this.
            • SilverWolvesGames

              This code is an excerpt of the player CharacterBody2D script from one of my projects. It worked well enough with an analog controller for what I used it for but it can surely be optimised.

              var swim_speed: float = 250.0
              var swim_acceleration: float = 0.1
              var water_resistance: float = 0.1
              
              func _physics_process(delta):
                     ##Get player input. Make sure you set these in the input maps
              	var input_direction: Vector2 = Vector2(Input.get_axis("Left","Right"), Input.get_axis("Up","Down")).normalized()
              
              	if input_direction.x > 0:
              		input_direction.x = 1
              	elif input_direction.x < 0:
              		input_direction.x = -1
              
              	if input_direction.length() > 0:
              		## Add 75 degrees to accommodate for player rotation head to be in front
              		var angle = input_direction.angle() + deg_to_rad(75)
              
                               ##Rotate agent to face input direction
              		rotation = lerp_angle(rotation, angle, 0.2)
              
                              ##Smoothly move player along the input direction with a slight acceleration
              		velocity = lerp(velocity, input_direction * swim_speed, swim_acceleration)
              
              		##Turn and flip agent depending on input direction
              		if input_direction.length_squared() >= 1 and !is_input_direction_equal_agent_direction(input_direction.x):
              			scale.x = input_direction.x
              	else:
                              ##If no input decelerate player movement
              		velocity = velocity.lerp(Vector2.ZERO, water_resistance)
              
              	set_velocity(velocity)
              	move_and_slide()
              
              func is_input_direction_equal_agent_direction(x_input_dir) -> bool:
                      ##Check if player is currently facing input direction
              	if x_input_dir == scale.x:
              		return true
              	else:
              		return false