I believe the problem is here:
+ Code Snippet if ObjectSphereSlide(FreeEnemy[x].id,oldx#,oldy#,oldz#,newx#,newy#,newz#,5.1)>0
newx#=GetObjectRayCastSlideX(0)
newy#=GetObjectRayCastSlideY(0)
newz#=GetObjectRayCastSlideZ(0)
SetObjectPosition(FreeEnemy[x].id,newx#,newy#,newz#)
endif
All the parameters of the sphere cast belong to the same npc, FreeEnemy[x]. The npc is checking for collision with itself. What you need to do is check for collision against all the other npcs.
The sphere cast code needs to look more like this (Note that the object that the sphere cast checks changed to "y")
+ Code Snippet for y = 0 to FreeEnemy.length
if ObjectSphereSlide(FreeEnemy[y].id,oldx#,oldy#,oldz#,newx#,newy#,newz#,5.1)>0
newx#=GetObjectRayCastSlideX(0)
newy#=GetObjectRayCastSlideY(0)
newz#=GetObjectRayCastSlideZ(0)
SetObjectPosition(FreeEnemy[x].id,newx#,newy#,newz#)
endif
next y
The current code is not particularly efficient as it will do each check twice. For instance if there a 3 npcs, with FreeEnemy.length = 2
- On the first loop of x it'll check x=0 againt y=0, y=1 and y=2.
- On the second loop of x it'll check x= 1 against y=0, y=1 and y=2.
- On the third loop of x it'll check x= 2 against y=0, y=1 and y=2.
It does each check twice and also checks each npc against itself. So it's more efficient to do:
+ Code Snippet for y = x+1 to FreeEnemy.length
if ObjectSphereSlide(FreeEnemy[y].id,oldx#,oldy#,oldz#,newx#,newy#,newz#,5.1)>0
newx#=GetObjectRayCastSlideX(0)
newy#=GetObjectRayCastSlideY(0)
newz#=GetObjectRayCastSlideZ(0)
SetObjectPosition(FreeEnemy[x].id,newx#,newy#,newz#)
endif
next y
So,
- On the first loop of x it'll check x=0 against y=1, y=2
- On the second loop of x it'll check x= 1 against y=2
- On the third loop of x it'll check x= 2 but now y=3 which is higher than the maximum value of FreeEnemy.length so this will end the loop.
Hope this makes sense.