What Neil said.
It’s helpful to click on the warning/error in Unity, so it pops up the console with all warnings and errors, not just the most last one. Frequently, the first error will cascade and cause other errors, so if you fix the first one, the rest might go away.
As far as the “flags” thing goes, it used to hold the value returned by the Move() function. Anything starting with a capital letter and using () is a function, that can return values. If you look in the docs for CharacterController.Move, you see “function Move (motion : Vector3) : CollisionFlags”. This means you input a variable of type Vector3, and you get back a variable of type CollisionFlags. (If you don’t get anything back from a function, it will say “void” in the docs.)
Therefore, when you moved the character controller, any collision flags that resulted in doing so that frame were assigned to the variable “flags”. Since you’re not using the variable, you can just call the function and not assign the return value to anything, since you don’t care about it.
Another problem is that you’ve got “var controller : CharacterController = GetComponent(CharacterController);” in your FixedUpdate function, which means that variable will only exist inside the function. But you’re already declaring a variable called “controller” outside FixedUpdate, which means you can use it anywhere. So, you can get rid of that line since you don’t need that local variable.
You’ve got a couple of “if (controller.isGrounded)” statements…you only need to test for that once, so those can be combined.
Another problem is the air control, with “moveDirection = new Vector3(Input.GetAxis(“Horizontal”), 0, 0);”. The “moveDirection.y -= gravity * Time.deltaTime;” line says to decrease the y component of moveDirection every fixed frame, but you’re setting moveDirection’s y component to 0 whenever the controller isn’t grounded, by using Vector3. This means gravity isn’t going to work. One solution is to make a new direction and just add that on top of the existing moveDirection, instead of explicitly setting it.
So, this should work:
var speed = 6.0;
var jumpSpeed = 8.0;
var airSpeed = 3.5;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var controller : CharacterController;
controller = GetComponent(CharacterController);
function FixedUpdate() {
// If controller is grounded, move on x/z axes and check for jump button
if (controller.isGrounded) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection) * speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// If controller isn't grounded, add a local left/right direction
else {
var airDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection += transform.TransformDirection(airDirection) * airSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
@script RequireComponent(CharacterController)