Its always hard to get collision perfect.
i do something like this, sorry just a copy paste nothing generic:
… start of main loop …
oldx#=GetCameraX(1)
oldy#=GetCameraY(1)-phdiv#
oldz#=GetCameraZ(1)
…. Move your camera …
phdiv# = (playerheight#/3.0)
// place camera on terrain or on another object
newx#=GetCameraX(1)
newy#=GetCameraY(1) -phdiv#
newz#=GetCameraZ(1)
SetObjectCollisionMode(1,1)
groundy#=GetCameraY(1)
if ObjectRayCast(0,newx#,newy#,newz#,newx#,newy#-(playerheight#*2),newz#) > 0
groundy# = GetObjectRayCastY(0)+(playerheight#*1.45)
else
// ground not found apply gravity.
groundy# = groundy# - playerheight# - 0.2
endif
newx#=GetCameraX(1)
newy#=groundy#
newz#=GetCameraZ(1)
SetCameraPosition(1,newx#,groundy#,newz#)
SetObjectCollisionMode(1,0)
newx#=GetCameraX(1)
newy#=GetCameraY(1) -phdiv#
newz#=GetCameraZ(1)
wassliding = 0
realchecks = 0
for colcheckloop = 1 to 4
if ObjectSphereSlide(0,oldx1#,oldy1#,oldz1#,newx#,newy#,newz#,playerheight#/2.2 )>0 //1.70
// only check x,z we alreasdy got y
newx#=GetObjectRayCastSlideX(0)
newy#=GetObjectRayCastSlideY(0)
newz#=GetObjectRayCastSlideZ(0)
SetCameraPosition(1,newx#,newy#,newz#)
if gotstuck < 10
groundy#=GetCameraY(1)
SetObjectCollisionMode(1,1)
if ObjectRayCast(0,newx#,newy# -phdiv#,newz#,newx#,newy#-(playerheight#*3),newz#) > 0
groundy# = GetObjectRayCastY(0)+(playerheight#*1.45)
endif
SetObjectCollisionMode(1,0)
SetCameraPosition(1,newx#,groundy#,newz#)
endif
wassliding = 1
else
// this spot is free save it if it should get stuck at some point.
lastgoodx# = GetCameraX(1)
lastgoodz# = GetCameraZ(1)
colcheckloop = 4 // break out of loop
endif
realchecks = realchecks + 1
next colcheckloop
playerheight# = the height of the player you like.
I do multiply checks to find a spot for the camera , and save the last known good spot , this way if you somehow get stuck inside a object or something like that you can always move to lastgoodx#, (if realchecks is always 4 for sometime)....
Sometimes you need more then one check , to make sure that the new position is ok ( after you set the new groundy# ).
I use a ObjectRayCast to find the ground , this will work on objects and terrain so you be able to leave the terrain and move up on a objects stairs. ( also allow you to go into a tunnel without ending ontop of the objects , but stay in the tunnel ).
You need to adjust these to settings: ... ObjectSphereSlide ... playerheight#/2.2... and ...phdiv# = (playerheight#/3.0)... to fit your needs.
Also i ALWAYS make a dummy object ... debugsphere = CreateObjectSphere((playerheight#)*2.0,9,9) , and place it infront of the camera using the camera xyz , so you can SEE what ObjectSphereSlide is checking, and exactly know what to expect. this makes it MUSH easier when you make the collision.
Start by moving and doing collision on the debugsphere ( setting the camera just behind like 3rd view ) , this way you SEE what need to be changed. The CreateObjectSphere should be the exact same size as ...playerheight#/2.2.. * 2.0 ( ObjectSphereSlide is radius ).
Hope it helps.
best regards Preben Eriksen,