Can't get character to fall with my Character Controller script it stays in the air

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
    public float speed;
    public float jumpSpeed;
    public float airSpeed;
    public CharacterController player;
    private Vector3 moveDirection;
    private Vector3 jumpVelocity = Vector3.zero;
    


    // Use this for initialization
    void Start () {
        player = GetComponent<CharacterController>();
	}

    // Update is called once per frame

    void Update()
    {
        
        Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        if (player.isGrounded)
        {
            moveDirection.x *= speed;
            moveDirection.z *= speed;
            if (Input.GetButton("Jump"))
            {
                
                moveDirection.y = jumpSpeed;
            }
            else
            {
                
                moveDirection.y = 0;
                
            }
        }
        else
        {
            moveDirection.x *= airSpeed;
            moveDirection.z *= airSpeed;
           
            moveDirection.y += Physics.gravity.y * Time.deltaTime;
        }
        player.Move((moveDirection + jumpVelocity) * Time.deltaTime);
    }
}

Can you post an image of the inspector for you player controller?

1 Answer

1

This is the code I used… its yours but I changed the jump to space, i put it all in fixedUpdate (even though it isn’t physics based like a rigidbody, I think it will have less jitter), and I added in a literal for gravity multiplier. This works for me.

using UnityEngine;

public class TypingThing : MonoBehaviour
{
    public float speed;
    public float jumpSpeed;
    public float airSpeed;
    public CharacterController player;
    private Vector3 moveDirection;
    private Vector3 jumpVelocity = Vector3.zero;


    void Start()
    {
        player = GetComponent<CharacterController>();
    }

    void FixedUpdate()
    {

        Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        if (player.isGrounded)
        {
            moveDirection.x *= speed;
            moveDirection.z *= speed;
            if (Input.GetKey(KeyCode.Space))    // I just need space for testing
            {

                moveDirection.y = jumpSpeed;
            }
            else
            {

                moveDirection.y = 0;

            }
        }
        else
        {
            moveDirection.x *= airSpeed;
            moveDirection.z *= airSpeed;

            moveDirection.y += Physics.gravity.y * Time.fixedDeltaTime * 5f;    // 5f should be a gravity or something
        }
        player.Move((moveDirection + jumpVelocity) * Time.fixedDeltaTime);
    }
}

But, in your inspector, you do have a capsule collider. You don’t need that, get rid of it. Player controller handles everything for you. It could be the issue. If that isn’t the issue and still doesn’t fall, then you should look in your physics manager and look at your gravity setting… its possible you have no gravity.


Unfortunately, it still does not work. The character falls at an extremely slow rate and jumping still teleports for me. I tried using a tutorial player controller and it was all good with gravity and all but I want air control on my controller