|
I've added a character controller to a capsule and even though it has a rigidbody attached, it won't use gravity and fall to the floor. As soon as I disable it though, it does. I even tried making artificial gravity through scripting (the script uses flags to detect whether or not the player is touching the floor): ('controller' being the CharacterController) That didn't even work. Why is this happening? Edit Here's the whole movement script:
(comments are locked)
|
|
You can't mix Rigidbody and CharacterController in the same object - the object becomes crazy! You must use only one of them. The CharacterController is easier: it already has a isGrounded property, and you can start with one of the example scripts in SimpleMove (rotates with AD, moves with WS, has automatic gravity but can't jump or fly) or Move (moves with WASD, can jump, gravity is added by script) and modify it to suit your needs.
var speed : float = 3.0;
var rotateSpeed : float = 3.0;
function Start(){
rigidbody.freezeRotation = true;
}
function Update () {
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Calculate forward/backward velocity:
var moveVel = transform.forward * speed * Input.GetAxis ("Vertical");
moveVel.y = rigidbody.velocity.y; // but conserve gravity effect
rigidbody.velocity = moveVel;
}
Okay I've changed it to use controller.Move() and if(controller.isGrounded) but the character controller still doesn't fall. Not even with this code: I even put that as the first thing in the Update void and it won't change.
Jul 15 '12 at 03:02 AM
Overlord
That's weird - this script should work. Have you removed the rigidbody? Another thing: the CharacterController has some problems with childed colliders that touch the capsule - it "thinks" to be colliding with its own children, and may get stuck or move to weird directions.
Jul 15 '12 at 05:33 AM
aldonaletto
Nope! That didn't work either. And there was no rigidbody or other colliders on the original object. This is very strange. I'm going to post the whole movement script in my question.
Jul 15 '12 at 10:07 PM
Overlord
You're a victim of bad indentation: controller.Move is inside the if (controller.isGrounded) branch, where moveDir.y is always zeroed. By the way, the Rotate instruction is wrong too. Change these lines: ... controller.Move(moveDir * Time.deltaTime); //<- move this line... controller.transform.Rotate(rotDir * Time.deltaTime, rotationSpeed); // this instruction is wrong! } moveDir.y -= gravity * Time.deltaTime; } } to this: ... // modify the Rotate instruction to this: controller.transform.Rotate(rotDir * Time.deltaTime * rotationSpeed); } moveDir.y -= gravity * Time.deltaTime; controller.Move(moveDir * Time.deltaTime); //<- to this position } } Always indent your scripts with a tab or two spaces per indentation level - it's impossible to avoid mistakes without proper indentation!
Jul 16 '12 at 03:14 AM
aldonaletto
(comments are locked)
|
Perhaps try applying gravity after you set the moveDirection. Tried it after and before and everything in between. Maybe something in the code is stopping it, although I have found nothing.
Jul 15 '12 at 11:52 PM
Overlord
Semi stupid question but I must ask. Is PlayerStat.controllable set to true?
Jul 16 '12 at 12:25 AM
Avaista
Yeah, I permanently set it on true for testing to make sure.
Jul 16 '12 at 02:19 AM
Overlord
Did you see my answer below ?
Jul 16 '12 at 02:26 AM
Avaista
Yes sorry, would've commented on it but I don't have access to my computer at the moment. Just a stupid question here: how do you mark more than one answer as the one that solved it? I've seen it done before.
Jul 16 '12 at 04:06 AM
Overlord
(comments are locked)
|
|
Alright I think I got it. Lets just take out te content of that if statement and... Something be missing, lad.... I added an else and applied the movement to the Move, and it worked. Good Luck, and Have Fun! Side note, I just noticed that when I post answers and comments I have different names 0.0 Edit: Fixed
Jul 16 '12 at 12:42 AM
Avaista
(comments are locked)
|

If it works with the Controller disabled, do you have another collider on it(I mean other than the character Controller)
It is possible that the character controller is detecting that and thinks it is on the ground.
Side note, you know of SimpleMove, yes?
No other colliders. And no I am not familiar with SimpleMove.
Oh and also, it doesn't think that it is on the ground. "grounded" equals false.