I have a character who plays his walk animation when he walks, but what if he is behind a wall? He would look sort of goofy walking when hes in place. Is there a way I can have a property ("Moving") be toggled to 1 when hes changing his position (not rotation) in 3d space? Any helps is appreciated; I would like to have this in logic bricks if it is simple. Thanks!
-
1$\begingroup$ Seriously? Stop using integers and use Booleans for true or false situations. You can simply have it detect True or False, instead of 1 or 0. toggle then works on Booleans. $\endgroup$– X-27 is done with the networkCommented Mar 17, 2015 at 0:56
1 Answer
You already answered your question by your own:
Behavior: When moving play walk animation
This implies: When not walking -> do not play walk animation.
As you do not explicitly told the character to stop (e.g. by command) you need to detect any events that stop it from moving.
There are many options to detect such events:
- no movement requests from user
- getting other requests from user (e.g. jump, run, stand, crouch)
- hitting a wall
- floating (e.g. when falling, jumping)
- speed below a limit
I guess you want to know the last option. This is not possible with logic bricks. You need Python to activate actuators when the object does not move anymore:
import bge
controller = bge.logic.getCurrentController()
owner = controller.owner
limit = owner.get("limit", 0.001)
lastPosition = owner.get("_lastPosition")
position = owner.worldPosition.copy()
owner["_lastPosition"] = position
if lastPosition is not None:
travelledDistance = (lastPosition-position).length
if travelledDistance <= limit:
for actuator in controller.actuators:
controller.activate(actuator)
This needs to be activated all the time (enable True Level Triggering).
Attention
- this is inefficient and should be used at a very limited number of occurrences
- this does not differentiate between other situations when the character is not moving (e.g. sitting, sleeping)
- you can tweak the limit by setting a float property limit
-
$\begingroup$ "this is inefficient and should be used at a very limited number of occurrences" - I will use this a lot and I need it to be able to reply quick many times; is there a way to fix that? $\endgroup$ Commented Mar 27, 2015 at 0:19
-
1$\begingroup$ A more efficient way is to use other detection methods, such as a collision sensor. Obviously this will only work on expected collisions (e.g. the character stops when the front part is colliding with a wall). $\endgroup$– MonsterCommented Mar 31, 2015 at 12:27