Hey, I ran in a bit of an issue with the CharacterController. The update method is called twice, what causes problems with controlling the character. This is the shortest amount of code to cause this problem on my computer. I have my settings for the CharacterController like this: Center is at 0, 0.95, 0; Radius is 0.4; Height is 1.85; The character is slightly above the ground so it can't stick into it. Now if you'll apply this script to the GameObject and you'll run the scene, the debug console is telling you False and True. If you collapse the console output you see that False and True are added at nearly the same time, which means for me, in the same frame (There is only a really short delay)! Adding a bool wasCalled into the class and checking if the update-method was already called makes the program really laggy, so that's not a solution. Also I have no code affecting the script, this is a test script I'm using parallel to my real PlayerController, and yes, the real one is disabled! Code (CSharp): [RequireComponent (typeof (CharacterController))] public class PlayerController2 : MonoBehaviour { private CharacterController cc; private float verticalVelocity = 0; void Start() { this.cc = GetComponent<CharacterController> (); } void Update() { transform.Rotate (0, Input.GetAxis ("Mouse X") * UserPreferences.mouseSensitivity, 0); Debug.Log(cc.isGrounded); if (cc.isGrounded) { this.verticalVelocity = 0; } else this.verticalVelocity = -1; this.cc.Move(transform.rotation * new Vector3 (Input.GetAxis ("Horizontal") * 3, this.verticalVelocity, Input.GetAxis ("Vertical") * 3) * Time.deltaTime); } } I hope anyone could help me, SOP.
Update is called once per frame. FYI you don't need 'this.something = foo'. You really only need it when you have ambiguity between member names. Whats probably happening is cc.isGrounded or verticalVelocity are oscillating, or cc.isGrounded is unreliable - eg the collider is falling through the floor or something.
I'm with @LaneFox on this one. Your grounded check might be buggy. The other thing to check is that you haven't accidentally added the script twice or to two different GameObjects. Debug.Log will actually take a second parameter. If you set it up like this you can quickly verify that both the true and false are coming from the same object. Code (CSharp): Debug.Log(cc.isGrounded, this);
First, thanks for your answers. The script is only applied once and now I'm sure that my collider is causing this issue. Setting the GameObject 1 unit higher makes it fall and only print out "false". If it collides with my "map" (It's just a flat mesh) the console shows me both, true and false. Any ideas how to fix this?
Just in case you hadn't tried this before - if you need to step through one frame at a time (which would let you see if indeed something is called multiple times in one frame, which update shouldn't be) you can pause the game in the editor, then hit that step through button at the top next to play and pause button. Comes in handy all the time!