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 /
avatar image
0
Question by pol315 · Oct 23, 2013 at 05:48 PM · physicsjump

Jump Script being extremely "sticky", performance issue?

I've been following a tutorial series for First and Third person controllers, but I can not get my jump working correctly. I've seen other scripts where people set their jump up like mine, but mine doesn't have the intended behaviour. When I reset my verticalVelocity float to 0.0f, it seems to cause a major performance issue, and it takes me 5 or 6 key presses to actually jump (hence it being "sticky"). If I don't reset my verticalVelocity float, then every time I fall off of an object, I keep adding more and more gravity each fall, until I drop so fast it looks like I teleport.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class FirstPersonController : MonoBehaviour {
     
     public int movementSpeed;
     public int mouseSensitivity;
     public float UDClamp;
     public float jumpSpeed;
     
     float verticalVelocity = 0.0f;
     float rotUD = 0;
     CharacterController cc;
     
     void Start() {
         Screen.lockCursor = true;
         cc = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update () {
         //Rotation
         float rotLR = Input.GetAxis("Mouse X") * mouseSensitivity;
         transform.Rotate(0, rotLR, 0);
         rotUD -= Input.GetAxis("Mouse Y") * mouseSensitivity;
         rotUD = Mathf.Clamp(rotUD, -UDClamp, UDClamp);
         Camera.main.transform.localRotation = Quaternion.Euler(rotUD, 0, 0);
         
         //Movement
         float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
         float strafeSpeed = Input.GetAxis("Horizontal") * movementSpeed;    
         
         if(!cc.isGrounded) {
             verticalVelocity += Physics.gravity.y * Time.deltaTime;
         }
         
         if(cc.isGrounded) {
             //verticalVelocity = 0.0f;
             if(Input.GetButtonDown("Jump")) {
                 verticalVelocity = jumpSpeed;    
             }
         }
         
         Vector3 speed = new Vector3(strafeSpeed, verticalVelocity, forwardSpeed);
         speed = transform.rotation * speed;
         cc.Move(speed * Time.deltaTime);
                 
     }
 }
 

The commented out line where I set verticalVelocity to 0.0f is the one in question. Can anyone tell me why this is happening, or give me any insight on how to fix it? Thank you.

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 meat5000 ♦ · Oct 23, 2013 at 05:58 PM 0
Share

alt text

Left one is what you've got, right one is what you want.

curves.jpg (37.2 kB)
avatar image pol315 · Oct 23, 2013 at 06:02 PM 0
Share

Thanks for the hint, I'll give it a shot :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by pol315 · Oct 23, 2013 at 07:24 PM

This may not be the best solution to the problem but I fixed this one on my own. IF I get a better answer I will mark it as solved. The way that I did it was to create two booleans, one for jumpng, and one for falling. If the player is jumping, apply gravity as normal, but if the player walks off of a ledge and is not jumping, then set the gravity to 0.0f, then set falling to true so that you can then apply gravity to a falling player. Here's the code containing the answer :

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class FirstPersonController : MonoBehaviour {
     
     public int movementSpeed;
     public int mouseSensitivity;
     public float UDClamp;
     public float jumpSpeed;
     
     float verticalVelocity = 0.0f;
     float rotUD = 0;
     CharacterController cc;
     bool Jumping;
     bool Falling;
     
     void Start() {
         Screen.lockCursor = true;
         cc = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update () {
         //Rotation
         float rotLR = Input.GetAxis("Mouse X") * mouseSensitivity;
         transform.Rotate(0, rotLR, 0);
         rotUD -= Input.GetAxis("Mouse Y") * mouseSensitivity;
         rotUD = Mathf.Clamp(rotUD, -UDClamp, UDClamp);
         Camera.main.transform.localRotation = Quaternion.Euler(rotUD, 0, 0);
         
         //Movement
         float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
         float strafeSpeed = Input.GetAxis("Horizontal") * movementSpeed;    
         
         if(!cc.isGrounded) {
             if(!Jumping && !Falling) {
                 verticalVelocity = 0.0f;
                 Falling = true;
             }
             verticalVelocity += Physics.gravity.y * Time.deltaTime;
         }
         
         if(cc.isGrounded) {
             if(Jumping) {
                 Jumping = false;    
             }
             Falling = false;                
             if(Input.GetButtonDown("Jump")) {
                 verticalVelocity = jumpSpeed;
                 Jumping = true;
             }
         }
         
         Vector3 speed = new Vector3(strafeSpeed, verticalVelocity, forwardSpeed);
         speed = transform.rotation * speed;
         cc.Move(speed * Time.deltaTime);
                 
     }
 }
 

  
Comment
Add comment · Show 5 · 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 meat5000 ♦ · Oct 23, 2013 at 07:55 PM 0
Share

If it works as you like, it's all good :)

Out of curiosity, did you figure out what was causing your Left-Curve exponential speed gain? I'll give you a thumbs up if you did ;)

avatar image pol315 · Oct 23, 2013 at 08:04 PM 1
Share

That's actually the way that I wanted it to behave. I did a little bit of investigating using the two graphs that you posted and it helped me understand how the Unity physics work a little bit better though! $$anonymous$$y main problem was just that the jump functionality was blocked the way that I had it previously. I will admit I have a lot more to learn about the physics yet though :P Thanks for your help!

avatar image meat5000 ♦ · Oct 23, 2013 at 08:09 PM 0
Share

Well, you can have it for the effort :)

Another thing that is good for jump which perhaps gets overlooked is AddForce or AddRelativeForce Force$$anonymous$$ode.Impulse

avatar image pol315 · Oct 23, 2013 at 08:12 PM 0
Share

Thanks! I will look it up :D

avatar image meat5000 ♦ · Oct 23, 2013 at 08:15 PM 0
Share

You should have enough $$anonymous$$arma to vote on Questions, Answers and Comments now.

Enjoy :)

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

2.5D Platformer - Jump Question 1 Answer

2d grounded check problem? 3 Answers

ConfigurableJoint Anchors and nested RigidBodies 2 Answers

Create a leaf with it's natural motion acting upon gravity? 2 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