- Home /
Question by
tbehnz · Feb 20 at 09:18 AM ·
charactercontrollerjumpingcharacter movementdown
Cannot jump while running downhill,Character can't jump while moving down
I have a good portion of Brackeys fps character movement script implemented in a third person game I'm making. My character cannot jump while running downhill. Namely, here's what it looks like:
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
animator.SetBool("IsJumping", false);
}
The problem is that when I run downhill, the velocity on the y axis becomes much less than -2. I tried to grab the moveInput on the z axis:
//Storing the input with the new input system
moveInput = new Vector3(ctx.ReadValue<Vector2>().x, moveInput.y, ctx.ReadValue<Vector2>().y);
//I make a variable storing the z-axis input
float zInput = moveInput.z;
//I try to use both the y velocity and the moveInput on the z-axis to set
//isGrounded to be true
if (velocity.y > 0 && zInput == 1)
{
Debug.Log("Y velocity is " + velocity.y);
isGrounded = true;
}
That doesn't work. Here's the rest of the movement script. Help a man out!
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
private Camera _mainCamera;
private PlayerControls _controls;
private CharacterController _controller;
public Animator animator;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
[Space]
private float _groundedTimer;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float turnSpeed = 12f;
public bool isGrounded;
public Vector2 mouseInput;
public Vector3 animatorInput;
public Vector3 moveInput;
public Vector3 velocity;
public float speed = 3f;
void Awake()
{
//Locks the cursor to the middle of the screen
Cursor.lockState = CursorLockMode.Locked;
//Store the main camera in a variable for later use
_mainCamera = Camera.main;
//Store the player controls in a variable
_controls = new PlayerControls();
//movement keys used. This is an event
_controls.KeyBoard.WASDKeys.performed += ctx =>
{
moveInput = new Vector3(ctx.ReadValue<Vector2>().x, moveInput.y, ctx.ReadValue<Vector2>().y);
};
//Movement keys no longer used. This is an event
_controls.KeyBoard.WASDKeys.canceled += ctx =>
{
Debug.Log("WASD key unpressed");
moveInput = new Vector3(ctx.ReadValue<Vector2>().x, moveInput.y, ctx.ReadValue<Vector2>().y);
};
//Gets the input for jumping. This is an event
_controls.KeyBoard.SpaceBar.performed += ctx =>
{
Debug.Log("Space bar pressed");
if (isGrounded && velocity.y < 0)
{
animator.SetBool("IsJumping", true);
velocity.y = (float) Math.Sqrt(jumpHeight * -2f * gravity);
}
};
//Spacebar released. This is an event
_controls.KeyBoard.SpaceBar.canceled += ctx =>
{
Debug.Log("Space bar unpressed");
};
//Mouse horizontal and vertical input. This is an event
_controls.KeyBoard.MouseDelta.performed += ctx =>
{
//Store mouse input into a variable for later use
mouseInput = ctx.ReadValue<Vector2>();
};
//Store the controller in a variable
_controller = GetComponent<CharacterController>();
//Store the animator in a variable
animator = GetComponent<Animator>();
}
//Invokes this every frame
void Update()
{
float zInput = moveInput.z;
//Velocity at the Y-Axis
velocity.y += gravity * Time.deltaTime;
//Boolean value for checking if the player is grounded
bool groundedPlayer = _controller.isGrounded;
if (groundedPlayer)
{
_groundedTimer = 0.2f;
}
if (_groundedTimer > 0)
{
_groundedTimer -= Time.deltaTime;
}
//This checks if the player is on the ground
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
//If the player is grounded and the falling speed is a lot, reset the velocity at the y-axis to keep player grounded
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
animator.SetBool("IsJumping", false);
}
if (velocity.y > 0 && zInput <= 1)
{
Debug.Log("Y velocity is " + velocity.y);
isGrounded = true;
}
}
private void LateUpdate()
{
Move();
Look();
}
/// <summary>
/// Moves the character and plays running animations according to the input
/// </summary>
void Move()
{
Vector3 moveVec = transform.right * moveInput.x + transform.forward * moveInput.z;
_controller.Move((moveVec) * speed * Time.deltaTime);
_controller.Move(velocity * Time.deltaTime);
animatorInput.x = moveInput.x;
animatorInput.z = moveInput.z;
//SetFloat takes these parameters: name, value, dampTime, deltaTime.
//For keyboard input, ALWAYS add dampTime and deltaTime to the animator float values
//in your 2D blendtrees.
//Do the same for joystick controls for now, unless proven otherwise.
animator.SetFloat("InputX", animatorInput.x, 1f, Time.deltaTime * 10f);
animator.SetFloat("InputY", animatorInput.z, 1f, Time.deltaTime * 10f);
}
/// <summary>
/// Makes the character face the direction the camera is looking
/// </summary>
void Look()
{
//Plug into the y axis for the
float yawCamera = _mainCamera.transform.rotation.eulerAngles.y;
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(0,yawCamera,0),turnSpeed * Time.deltaTime);
}
//Enables and disables the controls. NEEDED.
private void OnEnable()
{
_controls.Enable();
}
private void OnDisable()
{
_controls.Disable();
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613053242im_/https://answers.unity.com/themes/thub/images/avi.jpg)