Third Person Camera


(ChaosEmergency) #21

-Comment rremoved-


(Noxury) #22

Great work Mobious! I’ve downloaded v2 and the Script runs very well but how I can turn the Player to the Cameras direction, when it moves? And i think there is a little bug: when you zoom in it seems the Camera is sliding to the right. I am trying to fix this, but with no result :confused:


(TheSojum) #23

man… thanks


(Nein01) #24

Hi Mobious, if you (or anyone else) has time to help me, I am wondering how I could modify this setup so that the camera stays behind the character unless the mouse is moved to look around?

I don’t have much experience with python, but I took your example file and just added WASD controls to the player. Then I thought I should try adding a camera actuator and set the target object to ‘Player’, but nothing happens, and parenting the camera to the player causes serious interference with the mouse look feature.

I hope I’m making sense… before I found this thread I just had the logic brick setup with WASD and camera targeting my character, but I had no zoom, mouse look or collision which is why I’m thankful for the skills of people who know scripting and example files like this one.

So, my question is basically how to combine the two?


(Mobious) #25

Replace the script with this one and don’t parent the camera to the player.
http://www.pasteall.org/47046/python


(Nein01) #26

wow, perfect!

Thank you so much; I am forever grateful.

if you aren’t too bothered, I have just one more question. Is there any way to make the camera automatically snap back behind the player once ‘w’/forward control is pressed?


(Mobious) #27

Yes, just add this to the end of the script (make sure you keep the indentation).


    if 0 < bge.logic.keyboard.events[bge.events.WKEY] < 3:
        own['x'] = player.worldOrientation.to_euler()[TRACKED_AXIS]

(Nein01) #28

Ok, sorry to keep being a noob, but I’ve still got issues. I didn’t post back right away cause I wanted to try to figure this out on my own… but I can’t seem to. For some reason it always turns my character around 180 degrees to face the camera. It did that with the monkey head too. Also, at seemingly random times the camera starts spinning around and the only way to make it stop it is to hit the left and right movement keys at the same time. :spin: Lastly, a couple of problems that are unique to my file are that collision isn’t working like it did with yours, and the camera no longer follows the turning of the character once the camera snaps back to position. It seems to snap back at random points around the character - not always at the same spot.


(Mobious) #29

The “TRACKED_AXIS” variable near the top of the script needs to be 2 (which corresponds to the z-axis which is the vertical axis of your character). For some reason you had it set to 1 (the y-axis).

Camera collisions are not automatic. If you want the camera to collide with an object, the object must contain the property “block_camera”.


(Nein01) #30

vielen dank, mobious. you’ve been a huge help to me! finally able to move forward with my project :slight_smile:


(Nein01) #31

Would it be very complicated to make this function on a sphere? I’m using a plantary gravity script from one of goran’s tutorials. As is, the camera doesn’t seem to turn or tilt relative to the character in the Z direction, so you end up looking at your character upside down when you get to the “south pole” of a sphere.

(I was thinking it would be more appropriate to bump this than to make a new thread… hope that’s correct.)


(Mobious) #32

It is doable, but would required the script to be modified in several ways. I’m working on other things at the moment, but I may work on updating this script in the future. In the mean time you could try modifying the code yourself if you want.


(ray2rush) #33

thank’s for the script,
i test to the cube as player and it’s fine,

i create trigger when i press q, the scene restart.
but when i restart the scene, the script won’t work.
how to fix it?


(Mobious) #34

When restarting the scene, the BGE apparently does not re-import or re-execute any .py files that us module execution mode. As such, the variables scene and player in the script attempt to use their original values and raise an error once the game is restarted. You can fix this by simply moving the lines that initialize those variables inside the main function.

# move these 2 lines (at line numbers 27, 28)
scene = bge.logic.getCurrentScene()
player = scene.objects['Player']

# controls
ZoomIn = bge.events.WHEELUPMOUSE
ZoomOut = bge.events.WHEELDOWNMOUSE
ToggleLR = bge.events.MIDDLEMOUSE

def main():

    # <-- down here inside the main function (make sure you properly indent)

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

(ray2rush) #35

ow i see,
so that line not need again outside the main?
well it’s work now,
this is the code

def main():
    
    # Change 'Player' to whatever object you want the camera to track
    scene = bge.logic.getCurrentScene()
    player = scene.objects['Player']
    
    cont = bge.logic.getCurrentController()
    own = cont.owner
    
    mouse = cont.sensors['Mouse']
    rear = cont.sensors['rear']
    front = cont.sensors['front']
    
    #this line for the restart
    restartScene11 = cont.sensors['restartScene11']
    restartSc11 = cont.actuators['restartSc11']
    if restartScene11.positive:
        cont.activate(restartSc11)


is it right?
thanks anyway.


(Joshk) #36

Hello. thanks for the script its very useful. But I wanted to make the character move in the direction of the camera when i press W or sumthin. I checked out the links in your previous comments but im a very bad python scripter and i didnt understand what it meant so a picture of how to set it up and step-by-step instructions would be very helpful. thanks. The script is very awesome and made my game so much better.


(Mobious) #37

Here’s a basic script that moves an object with the WSAD keys to get you started.

import bge
import mathutils

scene = bge.logic.getCurrentScene()
camera = scene.active_camera
obj = bge.logic.getCurrentController().owner
moveSpeed = .1

# get the movement vector
moveVec = mathutils.Vector([0, 0, 0])
keys = bge.logic.keyboard.events
if 0 < keys[bge.events.WKEY] < 3:
    moveVec.y += 1
if 0 < keys[bge.events.SKEY] < 3:
    moveVec.y -= 1
if 0 < keys[bge.events.AKEY] < 3:
    moveVec.x -= 1
if 0 < keys[bge.events.DKEY] < 3:
    moveVec.x += 1
moveVec.normalize()

# get the camera rotation matrix
camMat=camera.worldOrientation.copy()
if camMat.col[2].z < 0:
    invert = -1
else:
    invert = 1
camMat.col[2]=[0,0,1]
yAxis = camMat.col[1]
yAxis.x *= invert
yAxis.y *= invert
yAxis.z = 0
    
# rotate and apply the movement
moveVec.rotate(camMat)
obj.worldPosition += moveSpeed*moveVec

Just attach it to a keyboard sensor with True level triggering and All Keys selected on the object you want to move.


(Joshk) #38

Thanks, this helped me alot. :wink: Im a beginner with python and just started using python so is there any tutorials on the web that you recommend about python in Blender? Anyway, thank you so much. :evilgrin:


(Mobious) #39

For using Python in Blender, I would suggest looking at other people’s scripts. Look at each part and try to understand what it’s doing. Start modifying them and see if you can predict the effects. Once you feel you have a basic grasp of things, start making your own scripts from scratch.

If there’s something don’t know how to do, try searching the Blender Python API or the Python API. APIs are your best friends, and learning to use them should be one of your top priorities. Even just reading through parts of them can be very helpful as you will often discover useful methods and functions you didn’t know about that will make implementing certain features much easier. If you can’t find what you’re looking for in the API, then you can try searching the forums or making a post as a last resort.

If you want to learn more about Python and coding in general, codeacademyhas a pretty good basic Python tutorial to get you started. For more in depth programming questions, stackoverflowis probably the best resource.


(littlesprite) #40

Hey, I just wanted to let you know I really appreciate you making this script available. It has been extremely helpful since I have been fuddling around with camera controls for a while and trying to make a decent working one. Thanks a ton.