Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Controller Upward Movement Along Concave Surface 0 Answers

jump with character controller best way? 1 Answer

How to make character controller forward jump a certain distance? 1 Answer

Shift BHOP in Character Controller 0 Answers

attached.Rigidbody isn't working with a ChactacterController? -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges