- Home /
CharacterController.Move() is causing unwanted vertical movement . . . how to fix?
Alright, so the Move() function of the character controller has me stumped. I have a little capsule in my scene and I just wanted to get it moving around, so I wrote the code below. Here's the thing - when I press any two directions (up and right, left and up, etc), the Move() function of the CharacterController starts changing the y values of the character's position. I cannot for the life of me figure out why this is happening. Using transform.position += does not yield the same problem. Any help would be MUCH appreciated.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class DummyPlayer : MonoBehaviour {
protected CharacterController myCharacterController;
void Start () {
myCharacterController = this.GetComponent<CharacterController>();
}
void Update () {
ProcessInput();
}
private void ProcessInput()
{
float analogZ = Input.GetAxis("Vertical");
float analogX = Input.GetAxis("Horizontal");
Vector3 moveVect = new Vector3(analogX, 0, analogZ);
if(moveVect.y != 0 || this.transform.forward.y != 0)
{
Debug.Log(moveVect.y + " " + this.transform.forward.y); //never called
}
//this.transform.position += moveVect*Time.deltaTime*40f;
myCharacterController.Move(moveVect*Time.deltaTime*40f); //why does this affect my y value when the line commented out above doesn't?
}
}
Also, the variables are named analogX and analogZ because eventually I'd like to get input from a joystick - for now I'm just using the keyboard.
UPDATE:
So after some experimenting around with project settings, I added the following lines to the end of my ProcessInput() function:
if(myCharacterController.collisionFlags != CollisionFlags.None)
{
Debug.Log("I understand nothing anymore.");
}
then ran the program. When I perform the steps necessary to recreate my original problem, every few updates, I see the Debug statement in the console. Mind you, that aside from the default camera Unity adds when you start a project, I have nothing in the scene except for my childless, parentless capsule. I cannot fathom how it is colliding with itself. Please help!
What does it say when you check the `myCharacterCtonroller.velocity`? Something to do with gravity came to $$anonymous$$d as the CharacterController doesn't apply gravity automatically (http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.$$anonymous$$ove.html)
justinl - Well, I think that's a start in the right direction - I just checked myCharacterController.velocity and when I hold down "right" and "up," every 3 or 4 updates (not consistent) it has a non-zero y component (which is anywhere between 8.6 or so to about 17.7).
However, I still have absolutely NO idea why it's happening, as I too was under the impression that the $$anonymous$$ove() function of CharacterController applied no gravity. Even so, the y component is always positive. Hmm . . .
Yeah I'm not really sure what's going on either but I'm curious to find out the reason. Is the terrain completely flat?
Well, right now, I don't have any terrain and am just moving my capsule in free space. Earlier though, I was curious about whether the IsGrounded property had anything to do with it so I created a flat terrain and applied a small constant downwards velocity to my capsule - when I would move I'd stop being grounded and start to fly.
But again, right now the capsule is the only object in the scene (not including the default camera).
So if I wanted to re-create your scene, all I would have to do is start a new project, create a capsule, apply the character controller component onto it, and then the script you've copied and pasted above?
Answer by LunaArgenteus · Sep 18, 2012 at 03:35 AM
Hahaha, alright people, I found the answer - ready for the face-palm moment? The capsule WAS colliding with itself - I had forgotten that CharacterController creates its own collider, and thus forgot to remove the default collider that is attached when one creates a game object. Everything is working now - thanks to both the people who tried helping me before I figured it out!
The lesson of the story for those who have similar problems with a CharacterController - first check to see if your character has any other colliders on it.
Good call, just found myself doing the same mistake, your thread was a time-saver! ...face-palms self with a frying pan...
Answer by Seth-Bergman · Sep 17, 2012 at 08:24 AM
maybe add the line:
Vector3 moveVect = new Vector3(analogX, 0, analogZ); // after this
moveVect = transform.TransformDirection(moveVect); // add this line
...etc
http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html