- Home /
Character Controller Not Moving Locally
I'm sorry if this has been asked before but I couldn't find anything on this
I made a simple capsule with Rigid Body physics, a Capsule Collider and a Character Controller (in that order). I created a basic c# script to control it and the script works fine but if i rotate the capsule, it keeps moving the same direction. Basically it's moving globally and not locally like i need it to. So yeah if someone can help me out with this that'd be great!
public float mouseSensitivity = 5.0F;
public float walkSpeed = 100.0F;
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
mouseLook ();
processController ();
}
void mouseLook () {
float h = mouseSensitivity * Input.GetAxis("Mouse X");
transform.Rotate(0, h, 0);
}
void processController () {
Vector3 v = new Vector3 (0, 0, 0);
if (Input.GetKey ("w"))
v.z += walkSpeed;
if (Input.GetKey ("s"))
v.z -= walkSpeed;
if (Input.GetKey ("a"))
v.x -= walkSpeed;
if (Input.GetKey ("d"))
v.x += walkSpeed;
controller.SimpleMove (v * Time.deltaTime);
}
On your rigidbody, do you have the flags checked for freeze rotations?
Yes, I do. I've tried enabling and disabling them but still nothing.
Use Vector3.forward to get the forward direction of an object, relative to it's rotation, and use that in your movement calculation. So if you rotated the character 90 degrees so it was facing downward, then Vector3.forward would point in a downward direction.
you have walkSpeed as public, so the 100 set atop would be squashed by something in the Editor, you don't 0 in the Editor for walkSpeed?
Answer by Invertex · Jan 26, 2014 at 02:24 AM
You want transform.forward, not Vector3.forward. Vector3.forward will only give you worldspace forward. transform.forward will be local forward.
Your answer
Follow this Question
Related Questions
rotate 90% in local space 4 Answers
Accessing variables from seperate scripts 1 Answer
Make everything use "world space" coordinates? 0 Answers
Move in global/world coordinate 1 Answer