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 elforama · Jan 13, 2013 at 02:40 AM · jumpingcharacter controller

Character Controller teleporting to ground instead of falling

Hi, I have a character controller for my guy in a 2D platformer. It can jump normally with scripted gravity pulling it down over time. However, once I either A. Hit something above the character while jumping, or B. Walk off the edge of a platform, the character seems to teleport straight to the ground below it. Please help if you can, here is the code controlling vertical movement of the character:

void Update () {

         movement = Vector3.zero;
         HorizontalMovement();
         VerticleMovement();
     
         movement *= Time.deltaTime;
         //Add movement to CharacterController
         cc.Move(movement);
         
         if(cc.collisionFlags == CollisionFlags.CollidedAbove){
             velocity.y = Vector3.zero;//if we hit something above us, stop jumping upward
         }
     
 }
 
 void VerticleMovement(){
     if(cc.isGrounded){
         jumping = false;
         doubleJumping = false;
         velocity = Vector3.zero;//On ground, so reset velocity which is only used when in air
         
         if(controls.jump && jumping == false){//pressed jump button
             jumping = true;
             velocity = cc.velocity;
             velocity.y = jumpSpeed;
             controls.jump = false; //reset jump controls
         }
     }else{//Currently falling or in the middle of jumping upwards
         DoubleJump();
         if(controls.jump == true)//did user press jump while already jumping?
             controls.jump = false; //reset the jump
         velocity.y += gravity * Time.deltaTime;//update our verticle speed
     }
     
     if(cc.collisionFlags == CollisionFlags.CollidedAbove){
             velocity.y = Vector3.zero;//if we hit something above us, stop jumping upward
     }
     
     movement.y += velocity.y;//Add the updated velocity
     movement.y += gravity;// * Time.deltaTime;//add the effects of gravity
 }
 
 void DoubleJump(){
     if(controls.jump && !doubleJumping){
         velocity = Vector3.zero;
         jumping = true;
         velocity = cc.velocity;
         velocity.y = doubleJumpSpeed;//move upwards from last know upward velocity
         controls.jump = false; //reset jump controls
         doubleJumping = true;//we can't double jump again
     }
 }
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

1 Reply

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

Answer by cdrandin · Jan 13, 2013 at 04:19 AM

This is probably not good ethics to do this, but no one has seem to answer you and I am pretty tired. SO I will give you my code for my own 2D platformer. I haven't added collision for above to reset jump velocity, but you have it down anyway. So sorry I can't answer to your specific question.

 class TwoDPlatformController ( MonoBehaviour ): 
     //public     test  = TwoDPlatformControllerJumping()  Access another class will also appear in inspector
     public walkSpeed   as single     = 6.0F
     public runSpeed       as single     = 10.0F
     public jumpSpeed   as single     = 8.0F
     public gravity     as single     = 20.0F
     public doubleJumpEnabled  as bool= true
     public extraJump as int          = 1
     public autoRotate  as bool       = true
     public maxRotationSpeed as single = 360
     
     private moveDirection as Vector3 = Vector3.zero
     private curNumJump    as int     = 0
     private curSpeed      as single  = 0.0F
     
     internal jumped       as bool    = false
     
     private jumpingApexReached as bool = false
     
     private controller as CharacterController
 
     
     def Update():
         PlayerMovement()
         HandleEvents()
         
         
     def PlayerMovement ():
         controller = GetComponent(CharacterController)
         
         // While character is grounded allow to jump and horizontal movement
         // Player is restricted in jump movement.  One way jump if double jump disabled
         if controller.isGrounded:
             curNumJump = 0
             jumped = false
             
             // Assign run speed when pressing left ctrl
             if Input.GetKey(KeyCode.LeftControl):
                 curSpeed = runSpeed
                 
             // Assign default walk speed
             else:
                 curSpeed = walkSpeed
                 
             // Apply vector direction, only focusing on X- axis
             moveDirection = Vector3( Input.GetAxis('Horizontal') * curSpeed, 0, 0 )
             
             // If player has jumped
             // Allow player to single jump while in air
             // Check if player has reached jump apex, used for animation states
             if hasSingleJumped():
                 SingleJump()
                 hasReachedJumpingApex()
         
         // Player is in the air
         // Allow the player the option of extra jumping, already checks if extra jumping is enabled
         // Check if player has reached jump apex, used for animation states
         // TODO: Future implementation, allow user to shoot while in air
         else:
             if hasExtraJumped():
                 ExtraJump()
                 hasReachedJumpingApex()
             
         
         ApplyGravityForce()
 
         // Apply new vector direction to the character controller
         controller.Move( moveDirection * Time.deltaTime )
         
     def SingleJump ():
     // Immediately present number of allowed mid air jumps when landed
     // Allow player to jump
         moveDirection.y = jumpSpeed
         jumped = true
             
     def ExtraJump ():
     // Allow player the capability to jump multiple times in mid air
     //  Number of times is based on 'extraJump'
     //  Once limit is reach player must land before jumping again
     // When jumping horizontal movement is unlocked for that jump then it will be locked again
     
         if curNumJump < extraJump:
             moveDirection = Vector3( Input.GetAxis('Horizontal') * walkSpeed, 0, 0 )
             moveDirection.y = jumpSpeed
             curNumJump += 1    
             
     def ApplyGravityForce ():
         moveDirection.y -= ( gravity * Time.deltaTime )
         
     def HandleEvents ():
     // Events taken place are of action types: shooting, special attacks, interactable object, etc
         pass
         
     def hasSingleJumped ():
         return ( Input.GetButtonDown( 'Jump' ) or Input.GetKeyDown( 'w' ) ) 
         
     def hasExtraJumped  ():
         return ( Input.GetButtonDown( 'Jump' ) or Input.GetKeyDown( 'w' )  and doubleJumpEnabled )
         
     def hasReachedJumpingApex ():
         if jumped and controller.velocity.y <= 0.0:
             jumpingApexReached = true
             return jumpingApexReached
         
         else:
             if controller.isGrounded:
                 jumpingApexReached = false
                 return jumpingApexReached
         
     def getSpeed ():
         return moveDirection.x

singles meant float. walkSpeed as single is the same as float walkSpeed. The jumpApex is returning when the character starts to descend after a jump.

Hopefully it helps in some way and sorry if the code looks scary, it is suppose to be reader friendly.

Comment
Add comment · Show 1 · 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 elforama · Feb 17, 2013 at 05:28 AM 0
Share

Thanks, I was able to find out the problem.

When my character was jumping it's upwards velocity would counteract the rather strong gravity I had. However, when I walked off the edge of a platform the character didn't have the upward velocity to fight the gravity which the jump would normally provide. The gravity was so strong it looked like it was teleporting. In actuality it was just falling too fast to notice.

To fix this I just had to lower the jump velocity and the gravity. Or I had to apply an equal but opposite vector to the gravity if my character wasn't grounded and hadn't jumped.

I don't remember which one I used because I ended up redoing the entire thing to become a whole lot more usable.

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

Character controller jumping is unresponsive 0 Answers

Movement Script Causing Jump Upon Looking Up 2 Answers

My Character controller keeps jumping infinitely 2 Answers

Help with Character Controller Platform Physics 0 Answers

[SOLVED] 2D Character Controller gains velocity when colliding with a corner 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