Invalid Get Index normalized on base vector 3

Godot Version

4

Question

Hi! , I was making an enemy AI for a horror game and got the error "Invalid get index normalized on base Vector 3, heres the code if you need it

extends CharacterBody3D

var SPEED = 4
var jumpscareTime = 2
var player
var caught = false
var distance: float
@export var scene_name: String
var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)

func _ready():
player = get_node(“/root/” + get_tree().current_scene.name + “/Player”)

func _physics_process(delta):
if visible:
if not is_on_floor():
velocity.y -= gravity * delta
var current_location = global_transform.origin
var next_location = $NavigationAgent3D.get_next_path_position()
yellow arrow goes here var new_velocity = (next_location - current_location).normalized * SPEED
$NavigationAgent3D.set_velocity(new_velocity)
var look_dir = atan2(-velocity.x, -velocity.z)
rotation.y = look_dir
distance = player.global_transform.origin.distance_to(global_transform.origin)
if distance <= 2 && caught == false:
player.visible = false
SPEED = 0
caught = true
$jumpscare_camera.current = true
await get_tree().create_timer(jumpscareTime, false).timeout
get_tree().change_scene_to_file(“res://Scenes/” + scene_name + “.tscn”)

func update_target_location(target_location):
$NavigationAgent3D.target_position = target_location

func _on_navigation_agent_3d_velocity_computed(safe_velocity):
velocity = velocity.move_toward(safe_velocity, 0.25)
move_and_slide()

Hey!
Is the error telling you what line it’s on?
That will make the debugging process a lot easier as I’m not seeing the issue at a glance :slight_smile:

Looks like the one with the yellow arrow goes here. They should reformat that code.

1 Like

It should be .normalized(), you forgot the brackets :slight_smile:

2 Likes

Ok so this line: var new_velocity = (next_location - current_location).normalized * SPEED needs to be calling the normalized() method.
So: var new_velocity = (next_location - current_location).normalized() * SPEED

1 Like

Please use the formatted text for the code and detail your problem further, add the error shown in the console

The error “Invalid get index normalized on base Vector 3” occurs because you’re trying to use an index on a Vector3, which isn’t possible. Vectors themself don’t have indexes like arrays.

Instead of (next_location - current_location).normalized, use the built-in function direction(target_point) on your NavigationAgent3D:

var new_velocity = $NavigationAgent3D.direction(next_location) * SPEED

also you can directly call get_tree().create_timer without await and chain the timeout method to change scenes:

get_tree().create_timer(jumpscareTime, false).timeout.connect(func() = 
  get_tree().change_scene_to_file(“res://Scenes/” + scene_name + “.tscn”)
)

thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.