Unity Newbie Needs Some Coding Help

So I tried Unity Answers, and I tried contacting the tutorial creator and I’ve seemed to have stumped everyone at every turn so I’ve decided to try here.

I’m working on a tutorial series where you have a simple capsule as the character controller that is placed on a flattened cube to represent the ground. (Simple enough, right?)

I’ve scripted the character controller to be able to move forward, backward, left, and right. The problem comes when I try to get the character controller to jump. The input from the spacebar will not move the character controller. I’ve tried checked my code. I’ve tried debugging to make sure the jump variable is being read (it is and it still isn’t responding to the spacebar input). I’ve checked my spelling and syntax (had one error with a string name but that didn’t change anything). And I’ve remade the project 3 times to see if it was something I missed in the initial setup - this ended up with the same result - the character controller doesn’t jump.

When the tutorial creator first contacted me, I tried changing the last line of code from _controller.Move(direction * Time.deltaTime);
to _controller.Move(velocity * Time.deltaTime);

This caused the character controller to fall through the ground. I have the box collider set so the character controller should hit an object and not move through it. I tested this with a cube in the level and the character controller hit the cube and didn’t pass through.

I don’t really know what to do to get this to work. I feel this should be something I should know how to do since I would like to make a hack and slash game and jumping would be important.

Here is my code:

using UnityEngine;
using System.Collections;
 
public class PlayerMovement : MonoBehaviour 
{
    CharacterController _controller;
 
    [SerializeField]
    float _moveSpeed = 5.0f;
 
    [SerializeField]
    float _jumpSpeed = 20.0f;
 
    [SerializeField]
    float _gravity = 1.0f;
 
    float _yVelocity = 0.0f;
 
    // Use this for initialization
    void Start () 
    {
       _controller = GetComponent<CharacterController>();
    }
 
    // Update is called once per frame
    void Update () 
    {
       Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
       Vector3 velocity = direction * _moveSpeed;
 
       if (_controller.isGrounded)
       {
         if (Input.GetButtonDown("Jump"))
         {
          _yVelocity = _jumpSpeed;
         }
       }
       else
         {
          _yVelocity -= _gravity;
         }
 
       velocity.y = _yVelocity;
 
       _controller.Move(direction * Time.deltaTime);
    }
}

Any ideas? I’m really new at Unity so I’m not sure what else I can do outside of asking for help at this point.

Well, i just copied pasted your code and used velocity instead of direction int the _controller.Move() call and it works.
So the issue would propably be in your object setup.

If i understand you rught you have a box collider + character controller on your object? The character controller itself uses a capsule collider so you wouldnt need another collider there.

When you say a box collider and a character controller on the object, so you mean on the capsule? I currently have it set up so the ground has the box collider. The capsule doesn’t have anything added to it that is a collider. What is currently on the capsule is Transform, the capsule mesh, the mesh renderer, the character controller, and three scripts. Here’s a screenshot of what I have: lesson4 screenshot04 | Danielle M. | Flickr

Is there a way to add a collider to the capsule so it doesn’t fall through? If so, where can I find it?

i dont immediatly see anything wrong with what you’ve got.
Do not put a box collider on the character, that is incorrect and misleading
It is _controller.Move(velocity* Time.deltaTime); that you should use

However:

the upward y velocity is only applied to the chracter on the single frame that space is pressed. it then immediatly goes back to y velocity = gravity, so the character barely takes off the ground, before heading back down again.

Ok I’m getting slightly confused but since I’m still new at this I guess it’s alright.

So I don’t put a box collider on the capsule. I can definitely see that. I do have a box collider on the ground but it doesn’t seem to want to detect once I change the code to _controller.Move(velocity* Time.deltaTime); since my capsule falls through the ground. Is there something I need to add to either the ground or the capsule to stop the instant falling once the game runs?