(I’m really new at Unity! Please be forgiving!)
I’m working on a tutorial series where I have the Character Controller scripted to do some basic movement with the arrow keys and WASD with the spacebar set to make the player jump. Everything works except the jump. Not really sure how to fix this. Any ideas? Here’s 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);
}
}
I don't know if this will work. its just a guess but try capitalizing your string jump. IDK if I am right or wrong.
– tssWhat things have you tried to make this work? When you say the jump doesn't work, what happens.
– Graham-Dunnett@tss: Tried that. Sadly it didn't work.
– ytwithlove@Graham Dunnett: I've tried a few things. I tried to change the placement of the model in the scene. Moving it farther down to the ground only resulted in the character controller moving up to the top of the ground with a key press from the arrow keys or WASD with the spacebar still not working. I tried to change the move and jump speed values in the code and in the inspector with no success. Not really sure what else to change.
– ytwithlove