4.3.stable.official
I think i’m all the way cooked here. I’m trying to adapt a 2D script for context-based steering, and turns out i’m not capable of that.
For context, the script is supposed to first take the direction it wants to go (thats the Vector3 ‘Go’ in the set_interest function) and apply it to the interest array, then shoot 8 rays around the character to detect obstacles, then add up those two arrays for the final direction. The actual movement script is not included as it works and is irrelevant to this
@export var look_ahead = 2
@export var num_rays = 8
var ray_directions : Array[Vector3] = []
var interest : Array[Vector3] = []
var danger : Array[Vector3] = []
var chosen_dir = Vector3.ZERO
var go = Vector3.ZERO # Dont worry about this, go gets set to the point of interest by another script
func _ready(): # Resize the arrays to the number of rays we're shooting
interest.resize(num_rays)
danger.resize(num_rays)
ray_directions.resize(num_rays)
for i in num_rays: # In theory at least, this is supposed to get the direction each ray should point
var angle = i * 2 * PI / num_rays
ray_directions[i] = Vector3.FORWARD.rotated(Vector3.UP, angle)
func _physics_process(delta):
# Populate context arrays
set_interest()
set_danger()
choose_direction()
func set_interest():
for i in ray_directions: # This is supposed to get the direction i want to go (go) and populate the interest array based on it. This is where i get my first error because Im using rotated() wrong. It also says "invalid type Vector3 for a base of type Array[Vector3]
var d = ray_directions[i].rotated(Vector3.UP, rotation).dot(go)
interest [i] = max(Vector3.ZERO, d)
func set_danger():
var space_state = get_world_3d().direct_space_state
for i in num_rays: #shoot some rays. I'm obviously using the 3d rays wrong because i adapted this from 2d code, but i dont know how to fix it
var query = PhysicsRayQueryParameters3D.create(position, position + ray_directions[i].rotated(rotation) * look_ahead)
query.exclude = [self]
var result = space_state.intersect_ray(query)
danger[i] = Vector3(0, 0, -5.0) if result else Vector3.ZERO
func choose_direction():
for i in num_rays:
interest[i] = interest[i] + danger[i]
chosen_dir = Vector3.ZERO
for i in num_rays:
chosen_dir += ray_directions[i] * interest[i]
chosen_dir = chosen_dir.normalized()
It’s a mess, so thank you lots in advance if you decide to take a look at it.