My character keeps moving on his own even without any keyboard input

View post on imgur.com

As you can see from the video above, my character moves on his own constantly even without keyboard input. It’s simply guided by my Camera3D. I have a feeling I know the source of the error, the get_facing_direction function always returns the vector, (0, 0, 1) at minimum, so there is no ZERO vector at all. I tried forcing a ZERO vector by making use of if statements, so that if there is no keyboard input then use a ZERO vector as the direction vector. However, that didn’t work well since whenever I try clicking the keys it would follow the directions of the x and z axis, instead of following where the camera is facing.

extends CharacterBody3D

signal noInput

@export var speed = 5.0
@export var jump_speed = 4.5
@export var g = 12.81

var camera3dScript = Camera3D.new()

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

var theVelocity = Vector3.ZERO


func _physics_process(delta):
    
    $"../CameraPivot".position = position
    var direction = $"../CameraPivot/Camera3D".get_facing_direction() 
    # Add the gravity.
    # print(gravity)
    #var direction = camera3dScript.get_facing_direction()
    #print(direction)
    if Input.is_action_pressed("move_left"):
        direction.x -= 1
    if Input.is_action_pressed("move_right"):
        direction.x += 1
    if Input.is_action_pressed("move_up"):
        direction.z -= 1
    if Input.is_action_pressed("move_down"):
        direction.z += 1
        
    if direction != Vector3.ZERO:
        direction = direction.normalized()
        $Pivot.basis = Basis.looking_at(direction)
    
    theVelocity.x = direction.x * speed
    theVelocity.z = direction.z * speed
    
    if not is_on_floor():
        theVelocity.y -= g * delta
    elif is_on_floor() and Input.is_action_pressed("jump"):
        theVelocity.y = jump_speed

    velocity = theVelocity
    move_and_slide()

Above is the CharacterBody3D code, and below is the Camera3D code,

extends Camera3D

# Sensitivity of the mouse
var sensitivity = 0.1

# Rotation angles
var rotation_x = 0.0
var rotation_y = 0.0

func _ready():
    # Capture the mouse pointer
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func get_facing_direction():
    # The direction the camera is facing is the negative z-axis of the camera's transform
    #print(-global_transform.basis.z)
    return -global_transform.basis.z

func _unhandled_input(event):
    #print(event)
    if event is InputEventMouseMotion:
        var direction = get_facing_direction()
        #print("Camera is facing: ", direction)
        _rotate_camera(event.relative)
        

func _rotate_camera(mouse_delta):
    # Update rotation angles based on mouse movement
    rotation_x -= mouse_delta.y * 1
    rotation_y -= mouse_delta.x * 1
    
    # Clamp the x rotation to avoid flipping the camera
    rotation_x = clamp(rotation_x, -90, 90 )
    
    # Apply rotation
    rotation_degrees = Vector3(rotation_x, rotation_y, 0)

Any help is appreciated, and thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật