- Home /
Third Person Controller Design
Ive been working on setting up a third person controller for a few hours now and cant quite figure out how to do it the way Im looking for. If I use Vector3() to assign the GetAxis(), I set up the x and z axes precisely. No matter what, the character will move in strict x and z axes. I want the character to roam freely. So, if I set up the W key to move forward, I want that key to move forward no matter the direction of the character.
So, what I want is to have W and S to move the player forward and backward no matter which way they face. And I want the character to rotate left and right if I use the A and D keys. Does anyone have an algorithm or an example to help me get this moving? To be clearer, I want it to work like World of Warcraft player movements.
Thanks!
Answer by MarkFinn · Dec 03, 2012 at 05:21 AM
You can use
playercharacterobject.transform.forward
to get a vector 3 which is essentially the direction the character is facing (assuming your models/prefabs are sensibly aligned) and then multiply that by the speed you want to move (per frame or per fixed update), before adding it to the character's position.
E.g. : Stick this on any object in a scene to get the idea.
using UnityEngine;
using System.Collections;
public class DumbController : MonoBehaviour {
public float spd=10;
public float rotSpd=25;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A)){transform.Rotate(new Vector3(0,Time.deltaTime*rotSpd, 0));}
if (Input.GetKey(KeyCode.D)){transform.Rotate(new Vector3(0,-Time.deltaTime*rotSpd, 0));}
if (Input.GetKey(KeyCode.W)){transform.position+=(transform.forward*spd*Time.deltaTime);}
if (Input.GetKey(KeyCode.S)){transform.position-=(transform.forward*spd*Time.deltaTime);}
}
}
Very nice! Now here's the issue with this set up...he walks through the terrain. And no gravity is applicable.
This is kind of a seperate issue, but to start does the object have a rigidbody component attached?
Depending on the exact details of your game and approach you may have to replace the simple updating of the transform.position with
rigidbody.AddForce(transform.forward*spd);(Best done in FixedUpdate() rather than Update().)
When I was working on City of S$$anonymous$$m we had similar issues and ended up doing everything by hand ins$$anonymous$$d of using the Unity Physics.
Again, very helpful. =) But, check this. Is it bad to use a rigidbody and take away the character controller? I did that and replaced the CC with a capsule collider and left the code the way it was with the transform.position...its works perfectly. Is there anything I could be concerned with in the long run or could this be an efficient way to solve my issue?
Your answer
![](https://koobas.hobune.stream/wayback/20220613084859im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Maintaining Run on direction change (double tap using GetAxis) 0 Answers
How to increase a slowing down speed? 1 Answer
Unity 2d mobile movement 0 Answers
Input.GetAxisRaw("horizontal") not working 1 Answer
Animated Texture based on Movement 0 Answers