I have a python challenge that I haven’t been able to figure out yet.
Goal Overview:
Create third person camera movement for a “PlayerCube”.
What I have so far:
PlayerCube
object (in the scene) with an attachedbge.types.KX_PythonComponent
external python scriptPlayerCamera
camera object (in the scene)
The “PlayerCube” is just the default cube that has one cone-shaped side to indicate its front-facing direction.
This “PlayerCube” has an external python script attached to it as a game component (bge.types.KX_PythonComponent). Inside this KX_PythonComponent.update
(), python script is detecting when keyboard keys are pressed and handling WASD
movement for the “PlayerCube”.
Also, this KX_PythonComponent.update
() method, the rotation of “PlayerCube” is applied when the Mouse input moves on the horizontal (left / right) axis.
So the “PlayerCube” turns
with the Mouse movement
and moves
with the WASD
keyboard keys.
Besides the “PlayerCube”, object there is also a “PlayerCamera” in the scene.
Goal, (what I’m asking for help with):
Is there a way to ‘attach’ the “PlayerCamera” to the “PlayerCube” so that the camera is always looking at the ‘back’ of the “PlayerCube”?
More detail:
When the player moves the Mouse to turn “PlayerCube” (left / right) I also want the camera to rotate in a sphere…
…while always looking at the ‘back’ of the “PlayerCube”.
Stipulations
To make this challenging, is there a way to do this completely in python
- (
no logic bricks
and no python scripts attached to controllers in logic bricks
).
I’m trying to make everything happen inside KX_PythonComponent
rather than use any logic bricks or logic scripts, to make the script more portable, ie, just create your Player, your camera, then attach the game component to the player.
…
What I’ve Tried:
Here is a sample of some of the python code I’ve tried:
inside KX_PythonComponent.start()
(the game component attached to “PlayerCube”)
scene = bge.logic.getCurrentScene()
camera = scene.objects["PlayerCamera"]
scene.active_camera = camera
camera .useViewport = True
camera.setViewport(0,0,
int(bge.render.getWindowWidth()),
int(bge.render.getWindowHeight())
)
# applyRotation does nothing when previewing the game after pressing 'p' to play
camera .applyRotation((0, 0, .02), True)
but I have not had any luck even moving or rotating “PlayerCamera” / camera, at all.
Summary:
Is there a way to make the “PlayerCamera” act as the third person camera for “PlayerCube” with python only written inside KX_PythonComponent
?
You can access the source code and the .blend file here:
https://bitbucket.org/gmilligan/upbge-learning/src/master/game_files/06_camera_follows_player/
Thank you for any attempts you may make to solve this challenge!