Third Person MMO-like Camera and Movement System (C#)
UPDATE: Check out this script from my framework for something more robust.
How It Works
This system will check whether a Main Camera exists and create one if it doesn’t, so you can actually position your Main Camera wherever you want (just turn off this script during testing). It will then center the Main Camera on an object that is a child of the player object (identified by tag). The reason we don’t look at the player object itself is because the camera will then focus on the feet of the player. By focusing on a child object, we can always have our Main Camera focus on a particular part of the player, such as the head.
Setup
1. We will identify the focus of our Main Camera using tags, so it’s important to specify a tag via the Edit -> Project Settings -> Tags and Layers menu. By default, the script looks for a tag called “CameraFocus”.
2. Now we need to create this empty game object and position it. In the menus, use GameObject -> Create Empty. Make this object a child of your player object and then position it where you want the camera to focus.
3. Make five C# script files. Name them:
- thirdPersonController
- thirdPersonMotor
- thirdPersonCamera
- thirdPersonCameraHelper
- thirdPersonAnimator
Attach the thirdPersonController, thirdPersonMotor, and thirdPersonAnimator scripts to your player object.
4. Open each script and add the following code to each. Note that in thirdPersonCamera, thirdPersonMotor, and thirdPersonAnimator, there are some variables that may need to be modified to fit your project! (check the comments)
- thirdPersonController: http://pastebin.com/7zZUiNMA
- thirdPersonMotor: http://pastebin.com/dLVURvRy
- thirdPersonCamera: http://pastebin.com/Vy84iQSp
- thirdPersonCameraHelper: http://pastebin.com/VhjXcxRM
- thirdPersonAnimator: http://pastebin.com/6RdPzyEy
5. Make sure your player character and ALL child objects (except for the camera child object) has the tag “Player”. Also, set the Layer to “Ignore Raycast” and be sure to apply the layer to all child objects. This is because the system uses raycasts to handle occlusion clipping and adjustment. This is so when the character is obstructed by the camera, the camera will automatically zoom in. The script also causes the camera to zoom back out once the obstruction is no longer there.
And That’s It!
The result is a camera and movement system used in many modern-day MMOs such as World of Warcraft and Guild Wars 2. If you want to customize the character’s movement system, add the custom movement code to thirdPersonMotor.cs and any animation code to thirdPersonAnimator.cs
Super scripts!!! They are very helpful. There’s just one bug. The player only starts rotating with the right mouse button AFTER pressing A or D. I’m searching the problem but until now i have nothing found :/
Now i’ve found it. In the thirdPersonMotor there is a function SnapAlignCharacterWithCamera(). There i removed the if condition. Now it works great 😀
Thanks, awesome scripts!
And thanks for the fix Jay !
Hey, thanks for the scripts but I have one problem. I’m getting this single error saying: Assets/thirdPersonController.cs(4,14): error CS0101: The namespace `global::’ already contains a definition for `thirdPersonController’
Hello i have only one request if anyone can help
What i want to achieve is a TPS camera like this http://fenglee.com/game/aog/
Or Vindictus MMO if anyone played it
This is the best action type camera,and i want it,i can manage to code various parts but i just cant get my character to align to camera XZ vectors.
If you look at thatAoT minigame when you turn the camera around the character and then press WASD any direction the character will move relative to where the camera is facing.
How do i make my character direction vectors to align based on where camera is facing not just the forward vector,all of them , when i press A or D the character should move paralel to camera’s X axis when i press W or A character moves paralel to camerza Z axis regardless of the previous character rotation/position,it always turns accord to where camera is facing i dont know how to snap it like that.