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 /
avatar image
0
Question by ColouredCarnival · Jan 12, 2017 at 03:52 PM · c#movementscript.jumpingledge

jumping from ledge problem

My movement script allows the player to jump 0.1s after going off a ledge so that it seems more realistical. Otherwise he would just slide off the ledge as soon as he reaches it. I have a Capsule with CharacterController.

The problem is that the player wont jump sometimes when on ground. It happens just once in 20 times but it happens and it would be irritating if it'd happen at a crucial moment. I tried the code without the ledge tolerance and it works perfectly fine but does someone have an idea how to solve the problem or does someone have an alternative?

the tolerance part would be the lines with jumpAllowTime, jumpAllowTimeTrack, waitToLand and waitToLandTrack

 void Start()
     {
         player = GetComponent<CharacterController> ();
         transform.position = spawnPoint.transform.position;
 
         jumpSpeed = Mathf.Sqrt (2 * gravity * jumpHeight);
         verticalSpeed = -gravity;
         jumpAllowTimeTrack = jumpAllowTime;
         movementSmoothFactor = groundSmoothFactor;
     }
 
     void Update(){
         moveHorizontal = 0;
         moveDepth = 0;
         moveSpeed = 1.0f;
         moveSpeedAir = 0.6f;
 
         //input
         onGroundInput = new Vector3 (moveHorizontal,0,moveDepth);
         //normalizes the vector so that diagonal movement doesn't get too fast
         if (onGroundInput.magnitude > 1) {
             onGroundInput.Normalize();
         }
         targetGroundMovement = onGroundInput;
         //lets the character walk into the direction the camera is facing
         targetGroundMovement = playerCamera.transform.TransformDirection (targetGroundMovement);
         targetGroundMovement.y = 0;
         targetGroundMovement *= onGroundInput.magnitude;
 
         //gravity
         if (player.isGrounded) {
             movementSmoothFactor = groundSmoothFactor;
             jumpAllowTimeTrack = jumpAllowTime;
             waitToLandTrack -= Time.deltaTime;
             debugText.text = "grounded";
         }
 
         if (!player.isGrounded) {
             movementSmoothFactor = airSmoothFactor;
             verticalSpeed += Physics.gravity.y * Time.deltaTime;
             jumpAllowTimeTrack -= Time.deltaTime;
             waitToLandTrack = waitToLand;
             debugText.text = "not grounded";
         }
 
         if (waitToLandTrack < 0.0f) {
             verticalSpeed = -0.1f;
             jumpText.text ="not jumping";
         }
 
         //jumping
         if ((jumpAllowTimeTrack > 0.0f) && (Input.GetKeyDown(KeyCode.Space))) {
             verticalSpeed = jumpSpeed;
             jumpText.text ="jumping";
         }
     }
 
     void FixedUpdate () {
         /*currentMovement converges to targetGroundMovement with time 
         currentMovementVelocity gets processed on every function call*/
         currentMovement = Vector3.SmoothDamp (currentMovement, targetGroundMovement * speed, ref currentMovementVelocity, movementSmoothFactor);
         currentMovement.y = verticalSpeed;
         player.Move(currentMovement * Time.deltaTime );
     }
Comment
Add comment · Show 1
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 ColouredCarnival · Jan 13, 2017 at 04:20 PM 0
Share

anyone an idea?

1 Reply

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

Answer by LK84 · Jan 13, 2017 at 04:45 PM

it's hard to say because there are many relevant parts of the code not provided (when and where is player.isGrounded set) but I assume the occasional non-reaction occurs due to getting Input in Update () and making the player to move/jump in the FixedUpdate(). It can occur that Update() gets called twice between two FixedUpdate() calls so your verticalSpeed value might get overwritten before FixedUpdate() is called (as far as I understand that's the value you are using to make the player jump)

Comment
Add comment · Show 3 · 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 ColouredCarnival · Jan 13, 2017 at 05:48 PM 0
Share

you sir.......thank you very very very much :D I didnt think that the values of the variables would be necessary but youre right, it would have been better to post them too and the isGrounded attribute is from Unity's Character Controller. I put the last part of the script into the update function and now there seems to be no problem :D But isnt it recommended to use the $$anonymous$$ove() function in LateUpdate() or something? I split up those parts because the $$anonymous$$eyboard Input needs to be in Update() and the physical part of the code with collisions etc. ($$anonymous$$ove() function) needs to be in LateUpdate() or so I thought?

avatar image LK84 ColouredCarnival · Jan 13, 2017 at 06:05 PM 0
Share

Yes, it's right in general to do Physic related stuff in FixedUpdate. You could use a bool, set in Update() to true and in FixedUpdate() to false and as long as the bool is true the value of the variable can't be overwritten. I converted my first comment to an answer since it helped you with your problem. I'd appreciate if you acceppt it ;)

avatar image ColouredCarnival LK84 · Jan 13, 2017 at 06:11 PM 0
Share

@L$$anonymous$$84: Alright now I'm able to proceed :D Of course I'll accept!!! Thanks for your advice!!!

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

8 People are following this question.

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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How to check if object is on ground (C#)? 1 Answer

Midair movement with the character controller? 5 Answers

CharacterController Jump from moving platform. 0 Answers

Movement Jumping Help 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