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.