#Character with NavAgent3D floating

1 messages · Page 1 of 1 (latest)

latent elbow
#

So I have this character, that moves to wherever i click on a map with a limited distance to travel. After tweaking the tree of it(Adding a camera with a characterBody3D and some other stuff), it started floating and moving in place wherever i sent it to. I'll drop the code, and screenshot from the nodes im using to test this (the one called "the hero" is the character itself).

extends CharacterBody3D

@onready var nav_agent : NavigationAgent3D = $NavigationAgent3D
@onready var path_predic : Node3D = $PathPredictor
@onready var pointer : Node3D = $Pointer
@onready var campivot : Node3D = $Pivot
@onready var cam : Camera3D = $Pivot/Camera3D
@onready var atk_area : Area3D = $AttackHitboxes
@onready var atk_hb : Array[CollisionShape3D] = [$AttackHitboxes/Cone, $AttackHitboxes/Cilinder, $AttackHitboxes/Sphere, $AttackHitboxes/Line, $AttackHitboxes/AreaCilinder, $AttackHitboxes/AreaCube]
@export var speed = 3.0
@export var camspeed = 20
@export var camsens = .025
@export var max_dist = 5
var path_point : PackedVector3Array
var is_moving : bool
var dist_unused = max_dist
var hp : int
var res : Array[String]
var vul : Array[String]

#
func change_atk(hb : int, size : Vector3):
    for i in atk_hb:
        i.disabled = true
    atk_hb[hb].disabled = false
    atk_area.scale = size

func apply_damage_to_others(tar : Array[CharacterBody3D], amm : int, type : String):
    for i in tar:
        i.apply_damage(amm, type)

func apply_damage(amm : int, type : String):
    var mult : int
    if type in res:
        mult = 0.5
    if type in vul:
        mult = 2
    hp -= amm * mult

func _unhandled_input(event):
    if event is InputEventMouseMotion:
        if Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE):
            campivot.move_pivot(event.relative.x, event.relative.y, camspeed)
            Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
            pointer.is_active(false)
        elif Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
            campivot.rotate_pivot(event.relative.x, camsens)
            Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
            pointer.is_active(false)
        else:
            Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
            pointer.is_active(true)

func _physics_process(_delta):
    # Make raycast from positon and intersect
    var ray_query = PhysicsRayQueryParameters3D.new()
    ray_query.from = cam.project_ray_origin(get_viewport().get_mouse_position())
    ray_query.to = cam.project_ray_normal(get_viewport().get_mouse_position())*300 + ray_query.from
    var result = get_world_3d().direct_space_state.intersect_ray(ray_query)
    velocity = velocity.move_toward((nav_agent.get_next_path_position() - global_position).normalized() * speed, .25)
    # If ray intersects