var localVillageManager
var wonderVector3 = Vector3()
var randomGenerator = RandomNumberGenerator.new()
func _ready():
	isWondering = true
	localVillageManager = get_parent_spatial()
	randomGenerator.randomize()
	wonderVector3 = Vector3(randomGenerator.randf_range(localVillageManager.global_transform.origin.x * 2, localVillageManager.global_transform.origin.x * -2), 0, randomGenerator.randf_range(localVillageManager.global_transform.origin.z * -2, localVillageManager.global_transform.origin.z * 2))
	print(wonderVector3)
	maleAdultNavigationAgent.set_target_location(wonderVector3)

I'm wondering if I've either miscalculated my radius attempt here or I'm doing something else wrong, I've got my agent wondering about a specific randomised amount now ( Yay ) but I had to use a random number generator because for some reason random.range wasn't playing nice with my code. This seems to be working but I'm pretty sure I've gone wrong with how far the agent is wondering.

  • Bimbam replied to this.
  • Lethn

    As your random range is a multiplication of your X and Z position values it is entirely possible for this to return from 0 to 0 (where X/Z = 0) and effectively do nothing.
    Also as it stands, the further away from world origin your localVillageManager node is, the larger your random range will encompass (by design?)

    I suspect what you were aiming for would be to replace those '*'s with '+'s which would effectively give you a random point in a square of size 2 around the localVillageManager node.

    If however you are after random point in a circle of a given radius around the localVillageManager node that is a bit different (Disk Point Picking) and would look something like:

    func random_pt_in_circle(radius = 1.0):
    	var r = sqrt(rand_range(0.0, 1.0)) * radius
    	var t = rand_range(0.0, 1.0) * TAU
    	return Vector2(r * cos(t), r * sin(t))
    
    func _ready():
    	isWondering = true
    	localVillageManager = get_parent_spatial()
    	randomGenerator.randomize()
    	
    	var random_pt = random_pt_in_circle(2.0)
    	wonderVector3 = Vector3(localVillageManager.global_transform.origin.x + random_pt.x, 0.0,localVillageManager.global_transform.origin.z + random_pt.y)
    	print(wonderVector3)
    	maleAdultNavigationAgent.set_target_location(wonderVector3)

    Lethn

    As your random range is a multiplication of your X and Z position values it is entirely possible for this to return from 0 to 0 (where X/Z = 0) and effectively do nothing.
    Also as it stands, the further away from world origin your localVillageManager node is, the larger your random range will encompass (by design?)

    I suspect what you were aiming for would be to replace those '*'s with '+'s which would effectively give you a random point in a square of size 2 around the localVillageManager node.

    If however you are after random point in a circle of a given radius around the localVillageManager node that is a bit different (Disk Point Picking) and would look something like:

    func random_pt_in_circle(radius = 1.0):
    	var r = sqrt(rand_range(0.0, 1.0)) * radius
    	var t = rand_range(0.0, 1.0) * TAU
    	return Vector2(r * cos(t), r * sin(t))
    
    func _ready():
    	isWondering = true
    	localVillageManager = get_parent_spatial()
    	randomGenerator.randomize()
    	
    	var random_pt = random_pt_in_circle(2.0)
    	wonderVector3 = Vector3(localVillageManager.global_transform.origin.x + random_pt.x, 0.0,localVillageManager.global_transform.origin.z + random_pt.y)
    	print(wonderVector3)
    	maleAdultNavigationAgent.set_target_location(wonderVector3)

      Bimbam Thank you! Will need to poke through this and read documentation. I had some random.range code going before but I think it was because it was in the context of vectors which weren't whole numbers and position data it wasn't playing nice at all with the agent movement when I tried implementing it.