- Home /
Third Person Character Controller Movement
Alright, this is sort of a two part question, so be ready if you plan to answer it.
I want to make a third person shooter. I have made a character model, worked in some animations, etc., and now I have worked on control/camera. I have: -modified MouseOrbit to do my bidding -modified ThirdPersonController to move relative to the player due to the previous and now I want to make the player move left/right instead of rotating on a press of A/D.
I want to know, in essence: -What part of the code does the ThirdPersonController handle input? I can't figure it out. -How can I change the rotation to a movement along Vector3's left/right?
Thanks in advance.
I just took a look at it and it seems to have moveDirection involved which I never completely understood but that variable handles how things move and all.
You could try gameObject.transform.Translate (Vector3.Right); and left for left. That'll move it right and left
Answer by Erisat · Jun 23, 2014 at 03:53 AM
http://www.3dbuzz.com/training/view/3rd-person-character-system VERY high-quality tutorial series, walking through step by step making a very decent third person camera, character system. They go through all the theory step by step, and explain every little line of code in grave detail, no matter how simple it may seem. This is where I started, and I'm still using parts of it.
anyway, This is how i handle strafing:
if(myRigidbody.velocity.magnitude < MoveSpeed() && Grounded >= 1)
{
myRigidbody.AddForce(PlayerGO.transform.forward VAxis MoveSpeed());
myRigidbody.AddForce(PlayerGO.transform.right HAxis MoveSpeed());
}
where MoveSpeed() just returns a float of how fast you should move. example my strafing speed is 3f; HAxis and VAxis are just:
HAxis = Input.GetAxis("Horizontal");
VAxis = Input.GetAxis("Vertical");
anybody whos gone through that tutorial will probably see where i've used stuff from 3dbuzz, but ive changed it to be using physics forces instead of charactercontroller.move
The above assumes that your character has a rigidbody attached, and a collider of some sort. The Grounded >= 1 is just a check to see whether your colliding with the terrain. I've also used parts of faux gravity scripts, but thats not really relevant to the strafing portion.
If you don't want to use the physics rigidbody and instead want just the plain old charactercontroller component, then check this out: http://docs.unity3d.com/ScriptReference/CharacterController.Move.html
you can see the line
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
the vector3 is (x,y,z), where x y and z are float values being fed by the keyboard input. x axis being left(negative) and right (positive), y being up(positive) and down(negative), and z being forward(positive) and backward (negative). this vector is then fed to charactercontroller.move, where it actually does the movement, Time.deltaTime makes sure that you move at a constant pace over time.
hope this helps
Thank you! It looks like I'll need to do some investigating into a custom character controller... Or maybe I'll rewrite the default? Who knows.
You could try out the premade character controller script from the unity Script Reference. http://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html