how can I improve on thiis gravity and jumps system? extends Node2D var gravity = 1 #…
- User
how can I improve on thiis gravity and jumps system?
extends Node2D var gravity = 1 # Gravity value
Called when the node enters the scene tree for the first time.
func execute(s, dir = []): s.velocity = Vector2(0,0) if dir.has("up") && GameManager.current_plane != "2D": s.velocity.y -= 1 if dir.has("down") && GameManager.current_plane != "2D": s.velocity.y += 1 if dir.has("left"): s.velocity.x -= 1 if dir.has("right"): s.velocity.x += 1
if dir.has("jump") && GameManager.current_plane == "2D": s.velocity.y -= 20 if GameManager.current_plane == "2D" && dir.has("gravity"): s.velocity.y += gravity if dir.size(): s.velocity = s.velocity * s.current_speed s.velocity.normalized() s.move_and_slide() - Assistant
- User
it stops the gravity when you move left or right:
func apply_gravity(delta): # Apply gravity to the player’s velocity if GameManager.current_plane == "2D": # Apply gravity to the player’s velocity velocity.y += gravity * delta
# Move the player and process collisions if self is CharacterBody2D: # Assuming you have a floor_normal set up correctly and a max_slides value move_and_slide() # You can reset y velocity if on the floor if is_on_floor(): velocity.y = 0 velocity.x = 0 else: # If the entity is not a KinematicBody2D, you’ll need another way to apply velocity. # It will depend on how your Entity class handles physics and movement. passextends Node2D
Called when the node enters the scene tree for the first time.
func execute(s, dir = []): s.velocity = Vector2(0,0) if dir.has("up") && GameManager.current_plane != "2D": s.velocity.y -= 1 if dir.has("down") && GameManager.current_plane != "2D": s.velocity.y += 1 if dir.has("left"): s.velocity.x -= 1 if dir.has("right"): s.velocity.x += 1
if dir.has("jump") && GameManager.current_plane == "2D": s.velocity.y -= 1 if dir.size(): s.velocity = s.velocity * s.current_speed s.velocity.normalized() s.move_and_slide() - Assistant
- User
- Assistant
- User
how to stop this from happeneing when you swap scene
func _on_swapPlane(): swap_plane = true PlayerState.set_last_2d_position(global_position) if GameManager.current_plane == "2D": GameManager.current_plane = "25D" SceneChanger.change_to_25d(global_position) # Calls the function to change to 2.5D scene else: GameManager.current_plane = "2D" SceneChanger.change_to_2d(global_position) # Calls the function to change to 2D scene swap_plane = false
func change_to_25d(player_2d_position: Vector2): # Save the 2D position if you need to retain it # Additional setup can be done here, such as setting the player’s position get_tree().change_scene_to_file("res://Scenes/Levels/prototype25D.tscn")
func change_to_2d(player_3d_position: Vector2): # Save the 3D position if you need to retain it # Additional setup can be done here, such as setting the player’s position get_tree().change_scene_to_file("res://Scenes/Levels/prototype.tscn")
- Assistant
When you're changing scenes using
get_tree().change_scene()orget_tree().change_scene_to()in Godot, your entire current scene tree is unloaded and replaced with the new scene. If you change the scene in the middle of an object's function call, such as during or , and that object tries to refer to a node that belonged to the old scene afterward, you will get an error because that node has been "freed" with the scene unload.