Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

rewrite this 3d character into 2d: extends CharacterBody3D classname Player3D enum State…

  1. User

    rewrite this 3d character into 2d:

    extends CharacterBody3D class_name Player3D enum State { IDLE, RUN, JUMP, ATTACK } var state = State.IDLE

    @export var movement_data : MovementData @export var stats : Stats

    func _ready(): stats.health = stats.max_health

    func _physics_process(delta): process_movement_input(delta) process_gravity(delta) process_actions()

    # Move and slide with snap when on floor, else without snap
    var snap = Vector3.DOWN * 20 if is_on_floor() else Vector3.ZERO
    self.velocity = velocity # Set the velocity property of the CharacterBody3D
    move_and_slide() # Call move_and_slide() without arguments
    update_animation_state()
    

    func process_gravity(delta): if not is_on_floor() or velocity.y > 0: velocity.y += movement_data.gravity * movement_data.gravity_scale * delta

    if is_on_floor() and velocity.y <= 0:
    	velocity.y = 0  # Stop vertical movement when on the floor
    

    func process_movement_input(delta): var input_vector = Vector2( Input.get_action_strength("move_right") - Input.get_action_strength("move_left"), Input.get_action_strength("move_back") - Input.get_action_strength("move_forward") ).normalized()

    if input_vector.length() > 0:
    	var basis = global_transform.basis.orthonormalized()
    	var direction = basis.xform(Vector3(input_vector.x, 0, input_vector.y)).normalized()
    	state = State.RUN
    	velocity.x = direction.x * movement_data.max_speed
    	velocity.z = direction.z * movement_data.max_speed
    	look_at(global_transform.origin + direction, Vector3.UP)
    else:
    	if state == State.RUN:
    		state = State.IDLE  # When there’s no input, and we were running, switch to idle state
    	velocity.x = lerp(velocity.x, 0, movement_data.friction * delta)
    	velocity.z = lerp(velocity.z, 0, movement_data.friction * delta)
    # Debug prints (optional)
    print("Input Vector: ", input_vector)
    print("Velocity: ", velocity)
    

    func process_actions(): if Input.is_action_just_pressed("jump") and is_on_floor(): perform_jump()

    if Input.is_action_just_pressed("attack"):
    	perform_attack()
    

    func perform_jump(): velocity.y = movement_data.jump_strength state = State.JUMP # Update state to JUMP when jumping is performed # Handle jump animation, sound etc.

    func perform_attack(): state = State.ATTACK # Set state to ATTACK # Add attack logic here …

    func update_animation_state(): match state: State.IDLE: # Code to update the player's animation state to idle pass State.RUN: # Code to update the player's animation state to run pass State.JUMP: # Code to update the player's animation state to jump pass State.ATTACK: # Code to update the player's animation state to attack pass

  2. Assistant

    Here's the 3D character rewritten into a 2D version:

    extends KinematicBody2D
    class_name Player2D
    enum State { IDLE, RUN, JUMP, ATTACK }
    var state = State.IDLE
    
    @export var movement_data : MovementData2D
    @export var stats : Stats
    
    func _ready():
        stats.health = stats.max_health
    
    func _physics_process(delta):
        process_movement_input(delta)
        process_gravity(delta)
        process_actions()
    
        # Move and slide with snap when on floor, else without snap
        var snap = Vector2.DOWN * 20 if is_on_floor() else Vector2.ZERO
        self.velocity = velocity # Set the velocity property of the KinematicBody2D
        move_and_slide(velocity, snap) # Call move_and_slide() with velocity and snap arguments
        update_animation_state()
    
    func process_gravity(delta):
        if not is_on_floor() or velocity.y > 0:
            velocity.y += movement_data.gravity * movement_data.gravity_scale * delta
    
        if is_on_floor() and velocity.y <= 0:
            velocity.y = 0  # Stop vertical movement when on the floor
    
    func process_movement_input(delta):
        var input_vector = Vector2(
            Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
            0 # 2D games typically only handle horizontal movement directly
        ).normalized()
    
        if input_vector.length() > 0:
            state = State.RUN
            velocity.x = input_vector.x * movement_data.max_speed
        else:
            if state == State.RUN:
                state = State.IDLE  # When there’s no input, and we were running, switch to idle state
            velocity.x = lerp(velocity.x, 0, movement_data.friction * delta)
        
        # Debug prints (optional)
        print("Input Vector: ", input_vector)
        print("Velocity: ", velocity)
    
    func process_actions():
        if Input.is_action_just_pressed("jump") and is_on_floor():
            perform_jump()
    
        if Input.is_action_just_pressed("attack"):
            perform_attack()
    
    func perform_jump():
        velocity.y = -movement_data.jump_strength
        state = State.JUMP  # Update state to JUMP when jumping is performed
        # Handle jump animation, sound etc.
    
    func perform_attack():
        state = State.ATTACK  # Set state to ATTACK
        # Add attack logic here …
    
    func update_animation_state():
        match state:
            State.IDLE:
                # Code to update the player's animation state to idle
                pass
            State.RUN:
                # Code to update the player's animation state to run
                pass
            State.JUMP:
                # Code to update the player's animation state to jump
                pass
            State.ATTACK:
                # Code to update the player's animation state to attack
                pass
    

    Note that this script now extends KinematicBody2D since we are working with a 2D character, and all 3D references (like Vector3) have been replaced with their 2D counterparts (like Vector2). Gravity has been adjusted to only affect the y-axis as there is no z-axis in 2D. The method global_transform.basis.orthonormalized() and related orientation logic has been removed since in 2D the direction is set simply by modifying the velocity x component.