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.
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);}
}
}