DEVLOG - 08 added an interesting navigation optimization script
i encountered some lag even with 4 enemies so sadly i had to optimize it on the bright side i saw yesterday a video about exactly that and omfg it saved me and explains the general idea of what i do its just that i do it worse and in a different way
video:https://www.youtube.com/watch?v=BPNr_txi6TU&t=813s
time:13:33(already in link)
how it works?
ok so its not the MOST OPTIMAL but it is good i mean i can every time an enemy die to resort it blah blah blah and use the modulo but this works good enough even though i spent half an hour fixing a bug that doesnt exist(turns out if you select a navigation region and bake navigation mesh it does that to all regions and not just the one you selected) but for some reason i did have to use a raycast down the enemy’s feet for it to work so they wont fall probably because of friction
so anyways here is the code:
extends Node3D
var tickAssigned = []
var tickCount = []
var currentTick = 0;
var tickTimer = 0.0
const TICK_RATE = 0.025
func _ready() -> void:
tickAssigned.resize(navObjects.size())
tickAssigned.fill(0)
tickCount.resize(20);
tickCount.fill(0)
var length = navObjects.size()
for i in tickAssigned.size():
tickAssigned[i] = -1
var num = 20.0 /length
var current = 0.0
for i in length:
if current >= 20:
current -= 20
tickAssigned[i] = int(current)
tickCount[int(current)]+=1
current += num```
# Called every frame. 'delta' is the elapsed time since the previous frame.
```func _process(delta: float) -> void:
tickTimer +=delta
if tickTimer < TICK_RATE:
return
tickTimer = 0.0
var tickDelta = TICK_RATE
var count = 0
for entity in navObjects:
if entity != null:
if tickAssigned[count] == currentTick:
var target = entity.target
if target != null:
var navAgent = entity.navAgent
var mode = entity.mode
var gotShot = entity.gotShot
var SPEED = entity.SPEED
var accelaration = entity.accelaration
var velocity = entity.velocity
if mode == "chase" or gotShot:
navAgent.target_position = Vector3(target.global_position.x,target.global_position.y,target.global_position.z)
var dir = (navAgent.get_next_path_position() - entity.global_position).normalized()
print(entity.name, navAgent.get_next_path_position())
entity.velocity = velocity.lerp(dir * SPEED, tickDelta * accelaration)
entity.look_at(Vector3(target.global_position.x,entity.global_position.y,target.global_position.z),Vector3.UP,true)
entity.shouldMove = true
elif mode == "attack":
navAgent.target_position = Vector3(target.global_position.x,target.global_position.y,target.global_position.z)
var dir = (navAgent.get_next_path_position() - entity.global_position).normalized()
print(entity.name, navAgent.get_next_path_position())
entity.velocity = velocity.lerp(dir * SPEED, tickDelta * accelaration)
entity.look_at(Vector3(target.global_position.x,entity.global_position.y,target.global_position.z),Vector3.UP,true)
entity.shouldMove = true
count+=1
currentTick +=1
if currentTick >= 20:
currentTick = 0
#@onready var body =$body
#@onready var explosionParticles = preload("res://Particles/enemyExplode.tscn")
#var particleInstance
#@export var navAgent : NavigationAgent3D
#@export var damage = 10
#@export var accelaration = 10
#var target
#var mode : String = "idle"
#var gotShot = false```
and the enemy code is too long so i cant put it here
also here is a clip of the current state of the game
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.