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
1
Question by junaum · Feb 02, 2012 at 11:31 AM · charactercontrollerjumpmove

Natural Jump algorithm

Hi,

I'm struggling to get my jump non linear. Right now, the character just rises and then falls, in very linear fashion. Using "Move", from character controller on other part of the code. The relevant part of the code is this:

 function nJump() {
     if (nJumping) {
     
         nNewPos.y += jumpSpeed * Time.deltaTime;    
                     
         if (!(nControl.collisionFlags & CollisionFlags.Above) && transform.position.y  < nOldPos.y + maxJumpHeight) {    
             nCanFall = false;                            
         }
         else if (nControl.collisionFlags & CollisionFlags.Above || transform.position.y > nOldPos.y + maxJumpHeight) {
             nJumpEnd = true;
             nJumping = false;
             nCanFall = true;
         }    
     } 
     
     if (nControl.isGrounded && nJumpEnd) {
         nGroundAfterJump = true;
         nJumpEnd = false;
     }
 }
 
 function nFall () {
     if (!nControl.isGrounded && !nJumping && nCanFall && !nCanClimb) { 
         nNewPos.y = -gravity * Time.deltaTime; 
         nFalling = true;
     }
     else if (nControl.isGrounded && !nCanClimb)    {
         nFalling = false;
         nCanFall = true;
     }    
 }

What's making everything linear is probably this:

nNewPos.y += jumpSpeed * Time.deltaTime;

and this:

nNewPos.y = -gravity * Time.deltaTime;

No matter what I try, I just get some weird accelerations and non desired behaviors. Can you help me there?

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 fafase · Feb 02, 2012 at 12:02 PM 0
Share

You could use a projectile motion function. You could try to develop a script starting from y = 1/2at^2 + v(0)t+ y(0). Look over the internet for detailed explanation.

Hope that helps

avatar image FLASHDENMARK · Feb 04, 2012 at 09:52 AM 0
Share

I was thinking something like an exponential decrease/increase in your movement ins$$anonymous$$d of a linear decrease/increase.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Feb 04, 2012 at 11:14 AM

Ok the way you handle your movement you can't really simulate accelerated movement. For this you need a speed variable that is changing over time.

So the usual procedure is to set or increase a speed variable to make the character move. To jump you just add a one-time vertical speed-boost. Gravity will bring your character down again.

 private var velocity : Vector3;
 var speed = 10.0;
 var jumpSpeed = 12.0;
 var gravity = Vector3(0,-9.81 ,0);
 
 function Update()
 {
     if (nControl.isGrounded)
     {
         velocity = Vector3(Input.GetAxis("Horizontal") * speed,0,0);
         if (Input.GetButtonDown("Jump"))
         {
             velocity.y += jumpSpeed;
         }
     }
 }
 
 function FixedUpdate()
 {
     if (!nControl.isGrounded)
     {
         velocity += gravity * Time.deltaTime
     }
     nControl.Move(velocity * Time.deltaTime);
 }

Accelerated movement should be done in FixedUpdate which runs at a constant framerate.

Basically it's like the basic example from the CharacterController reference page

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 junaum · Feb 08, 2012 at 06:22 PM 0
Share

Guys, you rock! Really. I've made progress and got my character to respect game speed, even with acceleration and also had it slightly more natural. Fixed update did the trick.

Got into 2 new problems now:

  • Fixed update is delivering a jagged / stuttered movement, it's not just "slow motion", it looks like slow frame rate.

  • I've managed to get Character Controller $$anonymous$$ove command out of fixed update and kept only the gravity addition there - the character falls smoothly (without the stuttering animation), but I can't do the same with the Jump variable (as it's a one time setup). So the Jump doesn't slow down at slow motion, at all.

I'm running in circles here lol

avatar image Owen-Reynolds · Feb 08, 2012 at 06:57 PM 0
Share

You can sometimes get a stutter if the camera movement code is in a different Fixed/non update as the player.

The "proper" way to perform move code is everything that changes over time (Controller.$$anonymous$$ove) in FixedUpdate, to make it frame-rate independant. $$anonymous$$eyboard reads have to be in Update, but they can just set vars that are used later.

avatar image junaum · Feb 08, 2012 at 07:14 PM 0
Share

Indeed everything (aside from move and position variables) are at FixedUpdate. Everything else, including input, are at Update. The camera is stationary right now, the "low frame rate" effect on movement (when in slow motion) seems to be something exclusively related to having the $$anonymous$$ove function inside FixedUpdate. Perhaps there is some kind of animation "Sample rate" that I must change when in slow motion?

avatar image junaum · Feb 08, 2012 at 08:09 PM 0
Share

I got the slow motion running correctly now (with $$anonymous$$ove) control. This is what I found out:

  • As you guys masterfully explained, every exponential calculation must be placed at Fixed Update.

  • Character controller "$$anonymous$$ove" must be at "Update", otherwise it does "low sample rate" the animation

  • Now here comes the trick: "$$anonymous$$ove" must be toned down, otherwise, it actually speeds up when in slow motion. This is my final move command: nControl.$$anonymous$$ove(nNewPos * Time.timeScale);

I guess that's it. It's working nicely now, but let me know if I'm doing something weird that will cause me trouble along the road.

avatar image hariszaman · Apr 04, 2013 at 12:16 PM 0
Share

junaum can you share the code that worked for you?

avatar image
0

Answer by junaum · Feb 04, 2012 at 04:44 AM

Thank you. I've researched some projectile motion stuff, and the "character move" sample from Unity Reference. Made some progress, but couldn't get my functions to work properly yet.

Right now, I got this for gravity: nNewPos.y -= gravity Time.deltaTime Time.timeScale;

And this for jump: nNewPos.y += jumpSpeed Time.deltaTime Time.timeScale;

They kind of work smoothly, but the jump accelerates a in the middle of the motion (instead of the begining)... and both animations don't respect Time.timescale (therefore, it can't be slowed down, or keep the same speed on different computers). I've tryied with the * Time.timescale, and without it.

Would you point me a direction to fix this, and still keep the smooth jump/grav effect?

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

9 People are following this question.

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

Related Questions

Two near-identical scripts cause a cat to jump in different ways - why? 2 Answers

controller.Move not working correctly 0 Answers

Control Mid-Air Movement With Character Controller 1 Answer

How can I set a timer? 1 Answer

Jumping with Character Controller?! 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