can't move around: extends CharacterBody3D enum State { IDLE, RUN, JUMP, ATTACK } var…
- User
can't move around:
extends CharacterBody3D
enum State { IDLE, RUN, JUMP, ATTACK } var state = State.IDLE
export var movement_data : Movement3DData export var stats : Stats
References
@onready var animation_tree : AnimationTree = $AnimationTree @onready var animation_state : AnimationNodeStateMachinePlayback = animation_tree.get("parameters/playback")
func _ready(): animation_tree.active = true 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 move_and_slide_with_snap(velocity, snap, Vector3.UP)func process_gravity(delta): if not is_on_floor(): velocity.y += movement_data.gravity * movement_data.gravity_scale * delta else: velocity.y = -0.01 # Very small sinking force to keep the player snapped 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() var direction = Transform(basis: global_transform.basis).xform(Vector3(input_vector.x, 0, input_vector.y))
if direction.length() > 0: velocity.x = direction.x * movement_data.max_speed velocity.z = direction.z * movement_data.max_speed # Rotate player to face the direction of movement look_at(global_transform.origin + direction, Vector3.UP) else: velocity.x = lerp(velocity.x, 0, movement_data.friction * delta) velocity.z = lerp(velocity.z, 0, movement_data.friction * delta)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 # Handle jump animation, sound etc.
func perform_attack(): # Set state to ATTACK state = State.ATTACK # Play attack animation animation_state.travel("Attack") # Add attack logic here …
func update_animation_state(): match state: State.IDLE: if is_on_floor(): animation_state.travel("Idle") else: animation_state.travel("InAir") State.RUN: animation_state.travel("Run") State.JUMP: if velocity.y < 0: animation_state.travel("Jump") else: animation_state.travel("Fall") State.ATTACK: # Handled separately in perform_attack()
Override method from player base to handle damage and death
func take_damage(damage: int): stats.health -= damage if stats.health <= 0: die() else: # Play damage animation, sound etc.
func die(): state = State.IDLE # Play death animation, particle effect etc. queue_free() # Only if you want to remove the player from the scene
Behind the scene methods like shooting or interacting with objects
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
still not working
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.