0
$\begingroup$

I found a 3D character controller setup online and one thing I wanted to ask is how to make movements smoother by easing velocity. Is there a way to edit the python script to make acceleration and deceleration less abrupt so that starting and stopping looks more realistic? e.g. taking half a second to reach to reach max velocity after clicking the move button. Thank you.

$\endgroup$

1 Answer 1

2
$\begingroup$

add 2 more properties to your character: speed and oldMove

enter image description here

you could use this script:

import bge
from bge import render

cont = bge.logic.getCurrentController()
own = cont.owner

scene = bge.logic.getCurrentScene()
empty = scene.objects["Empty"]
pointer = scene.objects["Empty.002"]

empty.worldPosition = own.worldPosition
move = 1

emptyrot = cont.actuators["rotz"]
playerRot = cont.actuators["playerRot"]
mouseMove = cont.sensors["MouseLook"]

keyboard = bge.logic.keyboard.events

W = keyboard[bge.events.WKEY]
A = keyboard[bge.events.AKEY]
S = keyboard[bge.events.SKEY]
D = keyboard[bge.events.DKEY]



if own["speed"] > 1:
    own["speed"] = 1


if W > 0:
    if own["oldMove"] == "W":
        own["speed"] += .02
    else:
        own["speed"] = 0
        own["oldMove"] = "W"
    own.alignAxisToVect(own.getVectTo(pointer)[1], 1, 0.15)
    own.applyMovement((0,own["speed"],0), True)
    own["timer"] = 0         
if A > 0:
    if own["oldMove"] == "A":
        own["speed"] += .02
    else:
        own["speed"] = 0
        own["oldMove"] = "A"
    own.alignAxisToVect(own.getVectTo(pointer)[1], 1, 0.15)
    own.applyMovement((-own["speed"],0,0), True)
    own["timer"] = 0 
if S > 0:
    if own["oldMove"] == "S":
        own["speed"] += .02
    else:
        own["speed"] = 0
        own["oldMove"] = "S"
    own.alignAxisToVect(own.getVectTo(pointer)[1], 1, 0.15)
    own.applyMovement((0,-own["speed"],0), True)
    own["timer"] = 0 
if D > 0:
    if own["oldMove"] == "D":
        own["speed"] += .02
    else:
        own["speed"] = 0
        own["oldMove"] = "D"
    own.alignAxisToVect(own.getVectTo(pointer)[1], 1, 0.15)
    own.applyMovement((own["speed"],0,0), True)
    own["timer"] = 0            
    
if own["timer"] >= 7:
    cont.activate(emptyrot)
    emptyrot.dRot = [0, 0, move/20] 
else:
    cont.deactivate(emptyrot)        
    
if mouseMove.positive:
    own["timer"] = 0 
$\endgroup$
1
  • $\begingroup$ Hello Chris, this is great! The only thing is that I've noticed if you hold two movement keys (forward, right) at the same time it halts movement for some reason, and if you press the same key (W) consecutively, the eased velocity effect disappears for that key until you press another movement key. Is there a way of avoiding these? Thanks for the help. $\endgroup$
    – Minato
    Commented Dec 25, 2021 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .