Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JoeKalango · Apr 08, 2019 at 04:00 PM · movementscript.jumpmovement scriptjumping

Help with jumping script

Hello everyone. I'm learning the ropes on C#, and while making a test game I've encountered a nice jump script to work with my idea, but the thing is: the jump is too smooth while going up and down. I've tried to fiddle with it by multiplying the _gravity by a float number instead of time.deltatime, but I was shocked to see that I plummeted to the ground like a rock. Since I'm only using a charactercontroller, should I put a rigidbody component to apply the jumpforce on the y axis, instead of the raw multiplication, or there is anything I can do inside the original script to make it a sharp, consistent jump?

Here is the original script joined with mine (only the part in the jump method):

 private CharacterController _controller;
 [SerializeField]
 private float _speed = 10f;
 [SerializeField]
 private float _jumpForce = 9f;
 [SerializeField]
 private float _gravity = 5f;

 private Vector3 _jump = Vector3.zero;
 
 // Start is called before the first frame update
 void Start()
 {
     _controller = GetComponent<CharacterController>();
 }

 // Update is called once per frame
 void Update()
 {
     Movement();

     Jump();
 }

 private void Movement()
 {
     float horizontalInput = Input.GetAxis("Horizontal");
     Vector3 direction = new Vector3(horizontalInput, 0, 0);
     Vector3 velocity = direction * _speed;
     velocity.y -= _gravity;
     _controller.Move(velocity * Time.deltaTime);
 }

 private void Jump()
 {
     if(_controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
     {
         _jump.y = _jumpForce;
     }
     _jump.y -= _gravity * Time.deltaTime;
     _controller.Move(_jump * Time.deltaTime);
 }

I've found out that multiplying the _jump.y and the _controller.Move method by 2 /2.5f makes the jump "sharper", but I'm open to suggestions. Also, is there any way to make the jump pressure sensitive? Like, depending on how long the space key is pressed, the shorter or higher the jump.

Thank you for your time.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KittyAnn · Apr 12, 2019 at 06:35 PM

@JoeKalango Here is my jump script. It uses the space bar to jump. You have UNLIMITED jumps (can basically fly if you do an animation for it). You need a RigidBody on your "player" & just attach this script to your "player". You can set the force in the 2nd last line: rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse); --- I have my force set to 5 (middle number controls the Y axis). Hope this helps! Here ya go:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class PlayerJumpScript : MonoBehaviour
 {
     Rigidbody rb;
     public bool isJumping = false;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();        
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             isJumping = true;
             rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
             GetComponent<Animator>().Play("isJumping");
         }
     }
 }
Comment
Add comment · Share
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
0

Answer by JoeKalango · Apr 19, 2019 at 01:41 AM

@KittyAnn actually it kinda did, but the problem is: The bool isJumping never gets deactivated, so the bool is set to true forever, so the player just floats around endlessly...

Comment
Add comment · Share
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

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

234 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

Related Questions

Need help with jumping on third person movement script. 0 Answers

Jump logic issues 0 Answers

Moving and jumping 1 Answer

RogueLike 2D (Tutorial) Rotation of Player 1 Answer

My jump scripit wont work. i have been tryingtt o make it work for an eternity. please help. 1 Answer


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