- Home /
CharacerController not allowing mid-air movement
I am attempting to implement a 3rd person platformer. A key aspect of platforming is the ability to control your character while in the air. I took the code from the Unity documentation on CharacterController.Move and I noticed that if I took the movement part of the code out of the if(player.isGrounded)
(so that the player could still move even if he was in the air), I couldn't jump. I tried a lot of things (changing values, lerping positions, and lots more) but I couldn't get the jumping working. Here's the code:
using UnityEngine;
using System.Collections;
public enum State
{
standing = 0,
walking = 1,
running = 2,
sprinting = 3,
jumping = 4
}
[RequireComponent (typeof(CharacterController))]
public class MovementController : MonoBehaviour
{
public float walkSpeed = 3.0f;
public float runSpeed = 4.0f;
public float sprintSpeed = 6.0f
public float strafeSpeed = 0.80f;
public float runThreshold = 0.5f;
public float maxSprintSeconds = 15.0f;
public float sprintCooldown = 1.0f;
public float sprintCooldownStart = 5.0f;
public float jumpForce = 8.0f;
public float airControl = 3.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController player;
private float horizontal;
private float vertical;
private State currentState = State.standing;
public void Start ()
{
player = GetComponent<CharacterController> ();
if (!player) {
Debug.Log ("There is no CharacterController component attached to the \"" + gameObject.name +
"\", and the MovementController.cs script depends on it!");
}
}
public void Update ()
{
if (!player)
return;
horizontal = Input.GetAxis ("Horizontal");
vertical = Input.GetAxis ("Vertical");
if (player.isGrounded) {
if (horizontal == 0 && vertical == 0)
currentState = State.standing;
else if (Mathf.Abs (horizontal) < runThreshold && Mathf.Abs (vertical) < runThreshold)
currentState = State.walking;
else if (Mathf.Abs (horizontal) > runThreshold || Mathf.Abs (vertical) > runThreshold)
currentState = State.running;
if (currentState == State.walking) {
moveDirection.Set (horizontal * strafeSpeed * walkSpeed, 0, vertical * walkSpeed);
}
if (currentState == State.running) {
moveDirection.Set (horizontal * strafeSpeed * runSpeed, 0, vertical * runSpeed);
}
if (Input.GetButton ("Jump"))
moveDirection.y = jumpForce;
} else {
currentState = State.jumping;
moveDirection.x = horizontal * strafeSpeed * airControl;
moveDirection.y = vertical * airControl;
}
moveDirection = transform.TransformDirection (moveDirection);
moveDirection.y -= gravity;
moveDirection *= Time.deltaTime;
player.Move (moveDirection);
}
}
Thanks in advance, for helping me. :)
Does the isGrounded bool actually change if the player is, you know, not on the ground?
Yeah, it's using CharacterController.isGrounded. (player is an instance of CharacterController)
Answer by Polymo · Sep 28, 2015 at 10:51 AM
Just wrap the IsGrounded only around the Jumpbutton-block.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
In air movement troubles. 1 Answer
Turning to face movement direction 0 Answers
C# Movement Script 3 Answers