1
Developer Created Tutorials / Example detection with raycast
« on: Nov 23, 15, 11:04:01 PM »
Due to request from some developers i'm gona copy/paste this code snippet. I use this snippet to detect ground proximity in my game with aircrafts. It basically takes a player characters, spawns raycasts in several directions around it and if the distance to heightmap is less than some arbitrary distance (i calculate this distance from aircrafts speed and acceleration) then i set ground proximity to be true.
Code: [Select]
method DetectGroundProximity ()
char as NodeRef = me._accControllerOwnerRef
pos as Vector3
GetNodePosition(char,pos)
leftVector as Vector3
GetNodeForwardVector(char,leftVector)
upVector as Vector3
GetNodeUpVector(char,upVector)
vectorList as List of Vector3
forwardVector as Vector3 = RotateVector(leftVector, (0,-90,0))
add back forwardVector to vectorList
rightVector as Vector3 = RotateVector(leftVector, (0,-180,0))
add back rightVector to vectorList
backwardVector as Vector3 = RotateVector(leftVector, (0,90,0))
add back backwardVector to vectorList
downVector as Vector3 = RotateVector(upVector, (180,0,0))
add back downVector to vectorList
leftDownVector as Vector3 = leftVector + downVector
add back leftDownVector to vectorList
forwardDownVector as Vector3 = forwardVector + downVector
add back forwardDownVector to vectorList
rightDownVector as Vector3 = rightVector + downVector
add back rightDownVector to vectorList
backwardDownVector as Vector3 = backwardVector + downVector
add back backwardDownVector to vectorList
excludedNodes as List of NodeRef = MUD_ExternalFunctions:MUD_GetClientCharacters()
add back char to excludedNodes
//excludedNodes as List of NodeRef
//add back myAccount to excludedNodes
distance2Ground as Float = 30
stoppingDistanceMultiplier as Float = 1
groundProximityDetected as Boolean = false
loop i from 1 to vectorList.length
intercept as Vector3 = (0,0,0)
meshName as String = ""
hit as NodeRef = Raycast3D(pos, vectorList[i], excludedNodes, distance2Ground, intercept, meshName)
if (hit <> None)
where hit is kindof heightmapnode
if(distance2Ground < (me.MUD_ACCCStoppingDistance * stoppingDistanceMultiplier))
groundProximityDetected = true
.
.
.
//reset distance
distance2Ground = 30
.
if (groundProximityDetected == true)
me.MUD_ACCCGroundProximityDetected = true
else
me.MUD_ACCCGroundProximityDetected = false
.
.