help me figure out why I can't move in 3d: extends CharacterBody3D enum State { IDLE,…
- User
help me figure out why I can't move in 3d:
extends CharacterBody3D
enum State { IDLE, RUN, JUMP, ATTACK } var state = State.IDLE
var velocity = Vector3.ZERO # Adding velocity as a member variable
@export var movement_data : MovementData @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()
code# 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) update_animation_state()func process_gravity(delta): if not is_on_floor(): velocity.y += movement_data.gravity * movement_data.gravity_scale * delta else: velocity.y = -0.01 # The small sinking force to keep the player snapped to the floor
func process_movement_input(delta): print("Input Vector: ", input_vector) print("Direction: ", direction) print("Velocity before: ", velocity) 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"))
codeif input_vector.length() > 0: input_vector = input_vector.normalized() # Normalize input_vector if it has a length var direction = -transform.basis.z * input_vector.y + transform.basis.x * input_vector.x 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) # Rotate player to face the direction of movement 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) - Assistant
Based on the code you've provided, there could be several factors preventing movement in your 3D character. Here are some troubleshooting steps and possible issues to examine:
- User
- Assistant
- User
Parser Error: No constructor of "Transform3D" matches the signature "Transform3D(Basis, Basis)".
- Assistant
My apologies for the confusion. The Transform3D constructor in Godot indeed does not take two Basis parameters. The Transform3D constructor in Godot expects a Basis and a Vector3 for the origin. Given that you want to convert the local movement vector to a global one using the basis, you can simply use the
basisproperty of the global_transform.Here is the corrected code for calculating the direction in the
process_movement_inputfunction: - User
- Assistant
The error you're encountering is due to misuse of the constructor. The constructor expects two arguments: a and a (representing the origin). In your existing code, you are incorrectly trying to use two objects to create a .
- User
Parser Error: Cannot find property "xform" on base "Basis".
- Assistant
My apologies for the oversight. It appears there has been a mistake in the provided guidance. The
Basisclass actually does not have anxformmethod. For transforming vectors using a , we use the operator to multiply the vector by the basis.