- Home /
Character Controller Stops Moving Forward in Mid Air
Note: I am using the First Person Character Controller (removed characterMotor.js and other control scripts).
I am having a problem with my automatic control system (this is for an infinite running game). When my character controller's vertical velocity reaches 0.0 while falling, my controller stops moving forward on the Z axis (default speed is 6.0 forward) although the moveDirection.z is still 6. I would like to be able to clean up my code to be more simplistic, as my character can only run in one direction and doesn't stop (unless it hits a wall).
Code: (undefined variables are defined before Update)
function Update()
{
//can I use strictly Move instead?
var forward : Vector3 = transform.TransformDirection(Vector3.left);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
transform.Rotate(0,0,Input.GetAxis ("Horizontal") * rotateSpeed);
transform.Rotate(0,0,Input.GetAxis ("Unhorizontal") * -rotateSpeed);
controller.SimpleMove(forward * curSpeed); //move player
//++STANDARD SETTINGS++\\
if (!isClimbing && !isSliding && !isRolling && !isTumbling && !isDead && !isHanging && isBalancing == 0)
{
moveDirection.z = 6.0; //run forward
}
if (controller.isGrounded && !isJumping)
{
Physics.gravity.y = -9.81;
isJumping = false;
moveDirection.y = -0.05; //keeps the controller grounded
}
else if (!controller.isGrounded && !isClimbing && !isHanging && !isDead)
{
isJumping = true;
Physics.gravity.y = -9.81;
moveDirection.y += Physics.gravity.y*Time.deltaTime;
}
//++JUMPING++\\ (swipe up --> moveDirection.y = 8.0 and isJumping = true)
if(isJumping)
{
if (Physics.Raycast (transform.position, Vector3.up, hit, 1.2))
{
moveDirection.y = 0;
}
}
controller.Move(moveDirection * Time.deltaTime); //move player minor
}
Sorry if the code is confusing; I'm omitting hundreds of lines to focus on the main character movement. Any help would be greatly appreciated!
P.S. I would love suggestions for a better way to control the player- modifying moveDirection often causes problems!
Well, first of all, I think that the best way of handling endless runner would be to keep the player still and move the assets, always co$$anonymous$$g in direction of the camera. That would be helpful for you in a fist moment. For floor, walls and stuff like that, you can simply change the texture's offset and it'll look like the player is moving.
Also, there are some IF statements that are verifying a lot of conditions. That makes the code very hard to understand. It's very possible that you're setting wrong a specific flag in a given moment. You should try to simplify it, reducing the amount of control variables you're using.
Other than that, you're setting the gravity in many different places, and always with the same value. Also, inside the update method. You could set it outside the update $$anonymous$$ethod, since the value is not being changed.
I know I didn't provided you a specific answer to your question, but, perhaps, if you think about my suggestions, it'll help you to find the problems easier in the future.
I'll consider that, thanks! However, I do allow the player to strafe right/left and occasionally turn 90 degrees. Does having so much code in the Update function cause a significant drop in performance?
well, to be honest, it's not much about performance, but it's not much elegant to handle this things I'm update. I suggest you to use events from an input handler and handle the behaviors in different methods.
for instance, in an input handler class, you detect a swipe to right. now you send the event and read it in the player behavior class, and make the player move to right. Does that makes sense?
I have a endless runner player behavior where I don't even use update method.