Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by ThePastaPriest · Aug 13, 2021 at 10:02 AM · scripting problem3djumping

How to get jumping to return to 0,0,0 quicker

I'll start off by saying this is my first week into trying to design a game through unity. I maybe have about 20 hours in unity at the moment, and have been just watching YouTube videos to create a basic character controller.

By piecing together the mechanics I learned, as well as adding some lines of my own; I was able to get a controller that had decent movement. My character moves, and looks with perfect precision and gravity, and the jump mechanic implemented jumps with perfect physics and gravity. However I've noticed upon landing I am not able to re-jump immediately. Upon investigating and changing certain stuff to the script, I realized that upon jumping, and then falling I would get to 1.27 (or something) in the speed I want then take approximately 1 second to settle back down to be able to clear the timer and jump again.

Essentially I'm at a loss trying to figure out how to get that last .02 to go a lot faster and just get to the ground. I've tried a bunch of various things, but I don't see anyone with this particular jumping issue so I would appreciate any help or guidance someone can bring. Here is the current movement script I am implementing....

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour { // Run and Crouch Serializations

 public bool isSprinting = false;
 [SerializeField] public float sprintingMultiplier;

 public bool isCrouching = false;
 [SerializeField] public float crouchingMultiplier;

 public bool isJumping = false;

 // POV and Player Movement Serializations

 [SerializeField] Transform playerCamera = null;
 [SerializeField] float mouseSensitivity = 3.5f;
 [SerializeField] float walkSpeed = 6.0f;
 [SerializeField] public float playerSpeed;

 [SerializeField] float gravity = -9.81f;
 [SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
 [SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
 [SerializeField] bool lockCursor = true;

 float cameraPitch = 0.0f;
 float velocityY = 0.0f;
 CharacterController controller;

 Vector2 currentDir = Vector2.zero;
 Vector2 currentDirVelocity = Vector2.zero;

 Vector2 currentMouseDelta = Vector2.zero;
 Vector2 currentMouseDeltaVelocity = Vector2.zero;

 // Jump Mechs

 private float groundedTimer;
 private float verticalVelocity;
 [SerializeField] private float jumpHeight;
 [SerializeField] private float jumpSpeed;
 [SerializeField] private float jumpForce;

 //More Hairy Shit
 private Vector3 moveDirection = Vector3.zero;
 private float directionY;


 // Start is called before the first frame update
 void Start()
 {
     controller = GetComponent<CharacterController>();
     if (lockCursor)
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
 }


 // Update is called once per frame
 void Update()
 {
     UpdateMouseLook();
     UpdateMovement();
 }

 void UpdateMouseLook()
 {
     Vector2 targetmouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

     currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetmouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);

     cameraPitch -= currentMouseDelta.y * mouseSensitivity;
     cameraPitch = Mathf.Clamp(cameraPitch, -90f, 80f);
     playerCamera.localEulerAngles = Vector3.right * cameraPitch;

     transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
 }

 void UpdateMovement()
 {
     //TestMovement
     bool isGrounded = controller.isGrounded;

     // Movement

     Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
     targetDir.Normalize();

     currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);

     if (isGrounded)
         velocityY = 0.0f;

     velocityY += gravity * Time.deltaTime;

     Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * playerSpeed + Vector3.up * velocityY;
     controller.Move(velocity * Time.deltaTime);

     velocityY += gravity * Time.deltaTime;

     //Hairy Shit

     bool ccGrounded = controller.isGrounded;
     
     if (isGrounded)
     {
         groundedTimer = 0.2f;
     }
     if (groundedTimer > 0)
     {
         groundedTimer -= Time.deltaTime;
     }

     if (isGrounded && verticalVelocity < 0)
     {
         verticalVelocity = 0f;
     }

     verticalVelocity += gravity * Time.deltaTime;



     //Running Jumping and Crouching Values
     if (Input.GetKey(KeyCode.LeftShift) && controller.isGrounded && !Input.GetKey(KeyCode.LeftControl))
     {
         isSprinting = true;
         playerSpeed = walkSpeed * sprintingMultiplier;
     }
     else
     {
         isSprinting = false;
     }

     if (Input.GetKey(KeyCode.LeftControl) && controller.isGrounded && !Input.GetKey(KeyCode.LeftShift))
     {
         isCrouching = true;
         playerSpeed = walkSpeed * crouchingMultiplier;
     }
     else
     {
         isCrouching = false;
     }
     if (isCrouching == false && isSprinting == false)
     {
         playerSpeed = walkSpeed;
     }

     if (Input.GetKey(KeyCode.Space))
     {
         if (groundedTimer > 0)
         {
             groundedTimer = 0;
             verticalVelocity = jumpForce;
         }

     }


     //Jump Mechanic

     Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
     controller.Move(jumpVector * Time.deltaTime);
 }

}

Comment
Add comment · Show 2
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
avatar image ThePastaPriest · Aug 12, 2021 at 05:10 PM 0
Share

I wanted to add that when I was referencing this 1.27 thats my y value. 1.25 is ground level, so that .02 takes approximately 1 second to settle back down to 1.25.

avatar image ThePastaPriest · Aug 12, 2021 at 10:53 PM 0
Share

I was actually able to solve my own question by tinkering around. Adding this script after the input that adds jumpforce to vertical velocity, I was able to get it to get to the 1.25 the way it was supposed to be. However I have noticed that now slopes are a little wonky. Is there any way to fix slopes like how the rigidbody controller has a raycast?

Here is the script I added;

if (Input.GetKey(KeyCode.Space) && controller.isGrounded) {

     }
     else
     {
         verticalVelocity += gravity * Time.deltaTime;
     } 

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

289 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 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

Can't get my character controller to jump properly (3d) 1 Answer

An object reference is required to access non-static member (CS0120) 1 Answer

Jump Script not Working JavaScript 1 Answer

No Gravity Jump 0 Answers

Character turning around itself when looked up or down! 0 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