I'm having problems with paths, especially when using a character controller with the object I intend to move. I'm making an RPG where you click to move the character and need to use a character controller for slopes and steps etc.
However, with the code I'm using it just causes the target object to teleport to the top of the world and then spin aimlessly, and when I click for it to go somewhere else it simply moves a little bit and then refuses to move any more, which is a major problem.
Here's my movement function:
FUNCTION Movement()
_Object = pick object( mousex(), mousey(), 1, 2)
if _Object = 1
position object 3, ( get pick vector x() + camera position x() ), ( get pick vector y() + camera position y() ), ( get pick vector z() + camera position z() )
if _MOUSE_BUTTON = 1
if _PLAYER_PATH_EXIST = 1 then ai delete path _PLAYER_OBJECT
ai make path between points _PLAYER_OBJECT, object position x(_PLAYER_OBJECT), object position y(_PLAYER_OBJECT), object position x(3), object position y(3)
_PLAYER_PATH_EXIST = 1
_PLAYER_PATH_CURRENT = 1
_PLAYER_PATH_COUNT = ai path count points(_PLAYER_OBJECT)
endif
endif
if _PLAYER_PATH_CURRENT>0 and _PLAYER_PATH_CURRENT<=_PLAYER_PATH_COUNT
phy move character controller _PLAYER_OBJECT, _PLAYER_SPEED#
_TARGET_X = ai path get point x(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT)
_TARGET_Z = ai path get point z(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT)
point object _PLAYER_OBJECT, _TARGET_X, object position y(_PLAYER_OBJECT), _TARGET_Z
if object position x(_PLAYER_OBJECT)=ai path get point x(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT) and object position z(_PLAYER_OBJECT)=ai path get point z(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT)
inc _PLAYER_PATH_CURRENT
endif
else
_PLAYER_PATH_CURRENT = 0
endif
ENDFUNCTION
and here's the full source:
sync on : sync rate 60
//set display mode 1024, 768, 32
//set window off
//Initialize global Variables
global _SOURCE_DIR$ as string
global _CAMERA_X_POS# as float
global _CAMERA_Y_POS# as float
global _CAMERA_Z_POS# as float
global _CAMERA_ORBIT# as float
global _CAMERA_ORBIT_MAX# as float
global _CAMERA_ORBIT_MIN# as float
global _CAMERA_ANGLE# as float
global _CAMERA_ELEVATION# as float
global _CAMERA_ELEV_MAX# as float
global _CAMERA_ELEV_MIN# as float
global _PLAYER_OBJECT as integer
global _PLAYER_SPEED# as float
global _PLAYER_DIRECTION# as float
global _PLAYER_X# as float
global _PLAYER_Y# as float
global _PLAYER_Z# as float
global _PLAYER_GRAVITY# as float
global _PLAYER_PATH_CURRENT as integer
global _PLAYER_PATH_COUNT as integer
global _PLAYER_PATH_EXIST as integer
global _MOUSE_BUTTON as integer
global _MOUSE_MOVE_X as integer
global _MOUSE_MOVE_Y as integer
global _MOUSE_MOVE_Z as integer
global _KEY_PRESS as integer
//Set Variables
_SOURCE_DIR$ = get dir$()
_CAMERA_ORBIT# = 25.0
_CAMERA_ORBIT_MAX# = 50.0
_CAMERA_ORBIT_MIN# = 25.0
_CAMERA_ANGLE# = 0
_CAMERA_ELEVATION# = 0
_CAMERA_ELEV_MAX# = 90
_CAMERA_ELEV_MIN# = 0
_PLAYER_OBJECT = 2
_PLAYER_SPEED# = 50.0
_PLAYER_DIRECTION# = 0
_PLAYER_GRAVITY# = 15.0
_PLAYER_X# = 0
_PLAYER_Y# = 0
_PLAYER_Z# = 0
_PLAYER_PATH_CURRENT = 0
_PLAYER_PATH_COUNT = 0
_PLAYER_PATH_EXIST = 0
//FlashLogo()
make object plane 1, 400, 400 : rotate object 1, 90, 0, 0
make object box _PLAYER_OBJECT, 4, 4, 4 : position object _PLAYER_OBJECT, 0, 5, 0
make object sphere 3, 2
phy start
ai start : ai set radius 1
for i=113 to 120
make object box i,rnd(10)+5,5+rnd(10)/10.0,rnd(10)+5
position object i,rnd(100)-50,2.5,rnd(100)-50
yrotate object i,rnd(360)
color object i,rgb(rnd(255),rnd(255),rnd(255))
AI Add Static Obstacle i
next i
ai complete obstacles
phy make rigid body static mesh 1
phy make box character controller _PLAYER_OBJECT, 0, 500, 0, 2, 2, 2, 1, 1, 70
ai debug show paths 4
DO
Inputs()
PositionCamera()
Movement()
phy update
ai update
sync
LOOP
FUNCTION Inputs()
_MOUSE_BUTTON = MOUSECLICK()
_MOUSE_MOVE_X = MOUSEMOVEX()
_MOUSE_MOVE_Y = MOUSEMOVEY()
_MOUSE_MOVE_Z = MOUSEMOVEZ()
_KEY_PRESS = scancode()
ENDFUNCTION
FUNCTION Movement()
_Object = pick object( mousex(), mousey(), 1, 2)
if _Object = 1
position object 3, ( get pick vector x() + camera position x() ), ( get pick vector y() + camera position y() ), ( get pick vector z() + camera position z() )
if _MOUSE_BUTTON = 1
if _PLAYER_PATH_EXIST = 1 then ai delete path _PLAYER_OBJECT
ai make path between points _PLAYER_OBJECT, object position x(_PLAYER_OBJECT), object position y(_PLAYER_OBJECT), object position x(3), object position y(3)
_PLAYER_PATH_EXIST = 1
_PLAYER_PATH_CURRENT = 1
_PLAYER_PATH_COUNT = ai path count points(_PLAYER_OBJECT)
endif
endif
if _PLAYER_PATH_CURRENT>0 and _PLAYER_PATH_CURRENT<=_PLAYER_PATH_COUNT
phy move character controller _PLAYER_OBJECT, _PLAYER_SPEED#
_TARGET_X = ai path get point x(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT)
_TARGET_Z = ai path get point z(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT)
point object _PLAYER_OBJECT, _TARGET_X, object position y(_PLAYER_OBJECT), _TARGET_Z
if object position x(_PLAYER_OBJECT)=ai path get point x(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT) and object position z(_PLAYER_OBJECT)=ai path get point z(_PLAYER_OBJECT, _PLAYER_PATH_CURRENT)
inc _PLAYER_PATH_CURRENT
endif
else
_PLAYER_PATH_CURRENT = 0
endif
ENDFUNCTION