# Tweening an object's world orientation

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

• Do you mean have a sort of delay? – VRM Nov 10 '15 at 23:56
• 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. – KozzyMoto Nov 11 '15 at 0:05

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:

'Cube2' logic: