Grapple Hook ‘Start point’
things implemented
fly to a place you aim at with shift + F and aiming
fly to a moving enemy = aim at enemy and shift + F
not implemented
(Real mouse look)
import bge
from mathutils import Vector
cont = bge.logic.getCurrentController()
own = cont.owner
if own['EffectTime']>0:
if 'head' not in own:
for child in own.childrenRecursive:
if 'head' in child:
own['head']=child
print('found head')
else:
if own['EffectTime']>=(120):
own['Target']="Empty"
else:
if type(own['Target']) is str:
print('Casting Ray')
AXIS = 1
## if head forward axis is X AXIS = 0
## if head forward axis is Y AXIS = 1
## if head forward axis is Z AXIS = 2
RANGE = 100
end = own['head'].worldPosition+(own['head'].worldOrientation.col[AXIS]*RANGE)
start = own['head'].worldPosition
Ray = own.rayCast(end,start,0,'',0,0,0)
## cast ray to find grapple target
bge.render.drawLine(end,start,(1,0,0))
if Ray[0]:
##if Ray[0] = did you hit something?
if 'Enemy' in Ray[0]:
own['Target']=Ray[0]
## is it a moving enemy ?(a object with the property enemy) if it is he is your target
else:
own['Target']=Ray[1]
## if the hit object is not a enemy grab the hit location rather then the enemy object as the target
else:
## if own['Target'] is not a string then it is either a enemy or a vector
Point = (0,0,0)
## to avoid issues with throwing a error
if type(own['Target']) is not Vector:
if not own['Target'].invalid:
Point = own['Target'].worldPosition
## if the target is a game object and did not get deleted set you target as where the enemy is
else:
own['Target']="Empty"
else:
## if the type is not a string (empty) or game object it is a worldPostion
Point = own['Target']
if Point!=(0,0,0):
VectorToTarget = Point-own.worldPosition
strength = 200
max_speed = 20
if own.worldLinearVelocity.magnitude<(max_speed):
own.applyForce(VectorToTarget*strength,0)
own.applyForce((0,0,9.8),0) ## cancel gravity
own.worldLinearVelocity*=.5
object 1 = player body (running script)
object 2 = player mouse look head (has property head)
keypress-------------and----------effect time=1
effect time=0-------/
if effect time min: 1 max: (end length-1)-------and-------add 1 to effect time
if effect time = end length -----------and------------effect time = - 120 (cool down)
if effect time is less then 0-------------and------------add 1 to time
(this is just to time the duration of the effect (you can always abort by setting the value of time to a negative number)
if effect time min 1 max end length----------python
Attachments
Grapple2.blend (519 KB)