Help ! Any programmers? Or good scripters out there that can help?

My task I want is quite simple, but I can’t seem to be able to do it. I want to be able to crouch when I press shift for example, but I DON’T want to animate it and use logic bricks. I want to script a movement that set’s my players camera dLoc from one position to another WITHOUT the teleportation aspect that comes in if I just do set dLoc, I want it to go slowly, more realisticly. Can someone help with this?
I was thinking maybe doing a for loop, but it won’t work… also I tried used it sort of incrementing using a timer that counts from lets say 1 to 100 sort of fast enough to seem smooth and not so jumpy, but I can’t really code this. Your help would be appreciated!

For loops give unexpected behavior, I offer my workaround for you to try if you so wish :smiley:

LikeAFor2.5.blend (331 KB)

LikeAFor.blend (128 KB)

I’m not sure this answers my problem… I really need a movement type of script that gradually moves something to another position without using an animation.

just use a timer and make it move a bit till it reaches the desired location

Yes, but how will the code look? I’ve tried that - and I don’t know of a solution that actually works.

It was more of ahelp you solve the problem, as you’d mentioned you tried with a for loop, and for loops don’t work properly, but if you integrate that file set-up into your script, it will execute as a loop.
I also have this:
smoothMove.blend (135 KB)
It can be tweaked to do exactly what you want.
This works with a for loop as they do work, but not correctly with actuators.

This is unchecked, but maybe something like:

import bge

cam = bge.logic.getCurrentScene().objects['Camera']

factor = 1
cam.worldLinearVelocity.z += factor

Apply the factor when you want the camera to move upwards. When the camera is at or near the target location stop the camera moving.


You can get as complicated as you want with this. Change the “factor” property to change how fast the camera approaches the required location, etc.

Edit2: I tried this in 2.57 and this accelerates the camera upwards. I guess you could call it “checked” now. You just need to make the mental :spin: acceleration do what you want now. :wink:


Oh yeah… The camera has to be a dynamic object instead of tthe default static for this to work. Static objects can only be teleported using dLoc since they are not physics objects. I hope this makes sense. :spin:

It says it’s missing a physics controller?

Can someone help me please with my camera crouching dilemma? Nothing has worked so far…

Blend file please?

I edited my original post, but the non-realtime nature of forums has lead to a bit of crossfire…

dynamic_cam.blend (61.1 KB)

Just play the game as usual to see the result. This is the default Blender scene with my 4 line script running on a dynamic camera so should be easy to figure out.

The render options have been set to “Blender Game” and in the physics options of the camera, the camera has been set to “Dynamic” so that velocity actually makes sense to the camera object. Velocity has no meaning for “Static” objects. This is the source of the “Missing physics controller” error.

Hmm - I can’t use a dynamic camera with my set up…

This simply won’t work. My camera can’t be dynamic.

You could always parent the camera to another object and use the script on that object. This would also effect the camera as the camera would be a child of that object. I didn’t think that the dynamic camera would be a magic bullet that would solve all your problems, it was merely a means for explaining how such an effect could be achieved.

It’s up to you to figure out the exact solution for your game. Every game is unique and every solution must also be unique.

I’m glad to have proposed a solution that might work and I’m happy that you’ve understood that solution, but nobody but you knows how you want the game to work. Welcome to the world of game programming. :smiley:

It’s a daunting world that many run away from screaming, but it’s really great fun.

My 4-line script is an example, not a solution. Very little funtional code is so small. For your game you will probably need to build this tiny script into something that achieves far more than the sum of it’s parts.

I wish you the best of luck, and can only say that you have the support of the entire community behind you if you are willing to stretch your own limits.

From my years on this forum, I can say that it is the most friendly and supportive community that I have found on the interwebs, but the more you put into your own projects, the more support you will recieve.

Then just use ‘teleportation’, but in a smooth manner. For example, say you want the Camera to go to point [0, 0, 10] smoothly:

from bge import logic

cont = logic.getCurrentController()
obj = cont.owner

endpoint = [0, 0, 10] # Ending position of the camera

diff = obj.position - mathutils.Vector(endpoint) # The difference between the current position and the end point

smooth = 0.9 # 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

I haven’t tested it, but it should work. Run that script on a Python controller hooked to an Always sensor, and it will slide on over to the end point.

@SolarLune: That’s a very nice way to introduce a smooth transition without using physics. I’ll try to remember this for my projects, but will most likely forget and need to ask for your help later :wink:

I tried it but it says mathutils is not defined…

@SolarLune: I also, I believe the problem with a endpoint vector is lets say I’m walking up a slope and now I want to crouch for example - there will also be new endpoints - I’m not sure if this makes sense - but in any case - I couldn’t manage to get your script to work SolarLune.

Oh, okay. Here’s another version that both should work, and may work relative to the player’s current position. Set it up to work from a sensor.

from bge import logic
import mathutils
cont = logic.getCurrentController()
obj = cont.owner

endpoint = obj.position.copy() # Ending position of the camera
endpoint.z -= 2

diff = obj.position - mathutils.Vector(endpoint) # The difference between the current position and the end point

smooth = 0.9 # 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

Okay, so the problem now is that it will just push the camera down - you’ll have to set the endpoint only when he isn’t crouching.

This still does not work unfortunately…

I’ve tried it - but it simply teleports directly to the position.