In my game I have a script that when I press W on the keyboard, the camera's rotation is set to my character's rotation. That works fine, but is there a way I can sort of "tween" the camera's rotation? Right now it's instant and doesn't look very good.

The script:

import GameLogic

cont = GameLogic.getCurrentController()
own = cont.owner # Camera
scene = GameLogic.getCurrentScene()

target = scene.objects['CharacterCollision']
up = cont.sensors['PressUp'] # Keyboard sensor

if up.positive:

     own.worldOrientation = target.worldOrientation
  • $\begingroup$ Do you mean have a sort of delay? $\endgroup$ – VRM Nov 10 '15 at 23:56
  • $\begingroup$ Well, sort of. I mean instead of the camera instantaneously changing it's orientation, to rotate a little at a time until it's orientation is that of the character. $\endgroup$ – KozzyMoto Nov 11 '15 at 0:05

Over three years since you asked, and with the BGE now dead, I will finally answer your question :-)

I understand what you mean by "tweening", but actually what is required is called lerping. Lerp means linear interpolation, and it is used to create a smooth transition between one value and another. One of its uses is making one object's rotation copy another object's rotation by some specified amount.

Here is the documentation on BGE's lerp()

It takes two parameters: other and factor

Other is the other thing to interpolate to, of like data type (in this case two object rotation matrices).

Factor is by how much - a float value in the range 0.0 to 1.0.

As for what to lerp, in this case it would be the worldOrientation of two objects.

In this simple example, let's take two cubes - one controlled by mouse movement, and the other lerping to match it. (Cube2 starts off rotated so you can immediately see it rotate to adjust)

Here's my script lerp.py containing my function lerp_fn(), being called through a Python Module Controller as lerp.lerp_fn:

import bge
scene = bge.logic.getCurrentScene()

def lerp_fn():
    target = scene.objects['Cube']
    lerper = scene.objects['Cube2']

    always = lerper.sensors["Always"] #Enable TRUE level triggering

    #'target' object also must have a Sensor conected to this Module in order to be recognized

    drag = 0.05 #How closely to copy the target rotation each tick (range 0~1)

    if always.positive:
        lerper.worldOrientation = (lerper.worldOrientation.lerp(target.worldOrientation, drag))

'Cube' logic:

The logic of the target cube

'Cube2' logic:

The logic of the lerping cube


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.