Third Person Camera

(ChaosEmergency) #61

So the camera collision does not work without the mouse movement ? i was just wondering :slight_smile:

(Mobious) #62

The code that updates the camera’s position takes mouse controls, zooming, and collisions all into account. So if you remove the ability to control the camera, you’ll need to rewrite that part.

You can leave most of the collision detection as is. It just sets the ‘dist’ property on the camera which tells it how far it should be from the player.

(ChaosEmergency) #63

How do you make the camera face the direction of the player ?
Like if you turn around.

(Mobious) #64

If you want the camera to snap to the position behind the player (so it is facing the same direction as the player’s +Y-axis), run the following near the bottom of the script:

playerVec = player.worldOrientation.col[1].to_2d()
playerAngle = playerVec.angle_signed(mathu.Vector([0, 1]))
own['x'] = playerAngle

(BluePrintRandom) #65

Nice! I never saw .to_2D() before,

I think it sounds like what he is aiming for, if if the camera angle is off by enough, and you press walk, to slide the camera back to
lined up, maybe :smiley:


3rdPerson.blend (502 KB)

(ChaosEmergency) #66

This is brilliant mobious. Only two problems im having. 1, i cant look around "I dont mind, but would be good if i could look left and right a tad more" 2, i tried HOR_OFFSET and its clipping through walls when i use lets say a value for my game ‘100.0’ so the
player is on the left hand side.

Would be great if there a way to fix this, my game would be perfect then :slight_smile:

That`s a good .blend BPR, but it does not use mobious script i see.

(BluePrintRandom) #67

it was just to demonstrate that when you move forward apply your code he just gave you.

if state == forward -> use mobius snippet. :smiley:

if you notice in mine I check to see if the angle is greater than a value

this leaves you room to mouselook while walking

(Mobious) #68

Replace the code I gave you with this:

playerVec = player.worldOrientation.col[1].to_2d()
playerAngle = playerVec.angle_signed(mathu.Vector([0, 1]))
if (own['x'] - playerAngle) % (2*math.pi) >= math.pi:
    testAngle = playerAngle - HOR_WINDOW
    if (own['x'] - testAngle) % (2*math.pi) > math.pi:
        own['x'] = testAngle
    testAngle = playerAngle + HOR_WINDOW
    if (own['x'] - testAngle) % (2*math.pi) < math.pi:
        own['x'] = testAngle

Note that you’ll need to define HOR_WINDOW yourself. It’s the angle you want to be able to look left/right in radians.

Again, fixing this would require refining the camera’s collision detection (e.g. by casting more rays, attaching a collision object to it, etc.). This isn’t something I plan on improving for this implementation. If you want, you can try implementing additional collision checks yourself.

(ChaosEmergency) #69

I tried that first code you ask me to replace with, it`s still not looking around, i indented them right.

(Mobious) #70

Make sure you defined HOR_WINDOW and check the console for errors.

(ChaosEmergency) #71

Thank you. Can you give me any help on how you define it as i`m struggling with it lol.

(Mobious) #72

Just put it near the top where all the other constants are defined. For example, if you want to the player to be able to look 45° degrees left and 45° right (90° total), use HOR_WINDOW = math.pi / 4 (π/4 radians = 45°)

(ChaosEmergency) #73

I tired this, i added I tired this, i added HOR_WINDOW = math.pi / 4
and it works great, i did not add radians part, do i need it ?

(Mobious) #74

No, you don’t need it. I was just explaining that π/4 radians is equivalent to 45° left and 45° right viewing angle.

(ChaosEmergency) #75

Oh my bad, it works a charm mobious, thank you for the great script.
I`m just figuring out how to make camera snap back behind the player
when moving, but not restrict from looking around when moving if that makes sense.

(YourBestFriend) #76

This looks great. I’ve had a few issues but I figured them out and now it works like a charm. I’m just wondering how I could make it so that the player character moves in relation to the camera, so W will rotate him so he faces directly away from the camera, S will make him face toward the camera, and A and D will make him face left and right in front of the camera, respectively, such as in the PC version of the Assassin’s Creed games. W,A,S, and D will all make him run forward, relative to the direction he is facing.

(YourBestFriend) #77

Thank you for responding quickly. That script worked great (once I finally realized I had to get rid of the original WASD keyboard controls I had in place already, that is–haha). However, it only did half of what I was looking for. It moved the character correctly, which is a good start given that I’ve been trying to figure out how to do that all week, but it did not rotate her to face the direction she moved in. Any ideas?

PS: After testing it some more, I noticed that if the camera is moved down to the ground, unless the camera is directly behind her she starts moving the opposite direction of whatever key is pressed (EG W makes her move towards the camera). Any advice would be greatly appreciated, but it’s not a matter of life or death; all else fails, I’ll limit the camera a bit more so it can’t move so low.

(YourBestFriend) #78

I’m brand new to this site (and forums like this in general) so I think I screwed up, being the technologically incompetent person I am, and my reply didn’t get posted correctly. If it did, then sorry for repeating myself.
Firstly, I wanted to thank you for replying so quickly. I asked my question when I went to bed and I was delighted to see it had already been answered when I woke up. You’re awesome.
Secondly, let me just say that the script was at least partially useful. It worked well to move my character in the direction the camera was facing. However, when the camera was angled lower than perpendicular to the character (such as when the player wishes to look at the sky), unless the camera was directly behind the character the controls were suddenly mirrored (EG W would make her run towards the camera rather than away from it). This issue was simply solved by limiting the angle of the camera so that it could not drop further than 90 degrees, but if you, in your infinite knowledge and mastery of python script, have a different solution that offers more freedom of control it would certainly be welcome.
Lastly, my biggest issue is that the character does not face the direction she is running. Pressing A or moving the mouse left, for example, will make her move left, but she will still be facing forward. Is there a way to fix this?

(Mobious) #79

Sorry, it looks like there was a bug in that script. Replace lines 29-32 with this:

yAxis = camMat.col[1]
yAxis.x *= invert
yAxis.y *= invert
yAxis.z = 0

If you want the character to face the direction it’s moving, you can add these lines to the end of the script:

if moveVec.magnitude:
    newQuat = moveVec.to_track_quat('Y', 'Z').to_matrix()
    obj.worldOrientation = obj.worldOrientation.lerp(newQuat, 0.1)

Note that the 0.1 value is the factor to use for the character’s rotation. You can adjust that between 0 and 1.0 to control how fast the character rotates.

(YourBestFriend) #80

You are just… an amazing human being. Thank you.