from bge import logic
import mathutils
cont = logic.getCurrentController()
obj = cont.owner
if not 'endpos' in obj:
obj['endpos'] = obj.position.copy()
endpoint = obj['endpos'].copy() # Ending position of the camera (beginning position's height - 2 Blender Units)
endpoint.z -= 2
diff = obj.position - endpoint # The difference between the current position and the end point
smooth = 0.5 # How smoothly to slide to the end position
obj.position.x += diff.x * smooth
obj.position.y += diff.y * smooth
obj.position.z += diff.z * smooth
Try this out. If it works, note that you’ll need to set the endpoint when your character isn’t crouched.
Okay. So, here’s the theory in action - press C to crouch, and release it to stand up. It does so smoothly, not ‘teleporting’ about. The script operates on the camera, so make sure you’re in that view to see it. The script is a bit more than necessary, but the theory should be able to be applied to your game.
Hmm - this doesn’t QUITE work… not yet atleast. We’re getting closer to a solution however… I’m uploading my blend file to show you why this isn’t working…
Firstly - this script will only work for a static camera… not a camera used for a FPS or something - (like what I want to design ) here’s the blend! I hope you can help further SolarLune, you’ve taught me a great deal already.
Okay, I ditched the whole ‘vector subtraction’ that I had in my script for a simpler subtraction of just the Z-axis variable. It works better this time around. Note that I deleted the empty and parented the Camera to a cube so that I could see what was going on - you can turn it invisible, but if you would rather keep your empty, then just use the script - I didn’t change anything else about the setup (I don’t think).
@SolarLune: Thank you very very much for your contribution, I’ve learned a lot from you SolarLune. I’ve learned enough to make my own script which works well. If you are interested, you can take a look below:
import bge
from bge import logic
from bge import events
cont = logic.getCurrentController()
obj = cont.owner
position = obj.position
key = logic.keyboard.events
SHIFTKEY = key[events.LEFTSHIFTKEY]
if not 'press' in obj:
obj['press'] = False
obj['return'] = False
limit = 1
if SHIFTKEY == 2:
if not obj['press']:
obj['press'] = True
if not obj['return']:
obj['initz'] = position[2]
obj['return'] = True
if obj['initz'] - position[2] < limit:
position[2] -= 0.1
else:
obj['press'] = False
if obj['return']:
if position[2] < obj['initz'] - .0001:
position[2] += .1
else:
obj['return'] = False