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 Lorcan · Jul 04, 2012 at 05:19 AM · movementspeedjumpingcharacter controller

How to fix horizontal movement code causing fast speed bug?

So I've been working with this jumping code and I can't for the life of me figure out what is causing/how to fix this crazy jump + movement bug I'm having. I'm hoping someone here can give me some help with this. Basically what I have set up is a character controller on the player with this script (code show below). The problem I'm talking about is that the player will move as if he's running really fast or something (hard to describe) when pressing the arrows keys and the jump button. In other words, the player will move really quickly horizontally when these buttons are pressed and the player is unable to jump in a diagonal fashion.

If I disable being able to jump while moving, that fixes the problem but then it eliminates the ability for the player to jump diagonally and he/she can only jump when not moving which is not what I want. This is a 2d platform game and the player must be able to jump diagonally so that he can actually get on platforms. The player cannot directly stand under a platform and jump, as if he does he will just collide with the platform and fall back down.

 //Global code
     @HideInInspector
     var CanMove : boolean = true;
     var jumpTimer = 1.0;
     var moveTimer = 2.0;
     @HideInInspector
     var HaveLives : boolean = true;
     @HideInInspector
     var firing :boolean = false;
     @HideInInspector
     var jumping : boolean = false;
     var Player : GameObject;
     var collisionFlags : CollisionFlags;
     
     // var mspeed = 6.0;
     var mspeed = 120;
     var jumpSpeed = 6.0;
     var gravity = 20.0;
     
     private var moveDirection = Vector3.zero;
     private var grounded : boolean = false;
 
     Player = GameObject.FindWithTag("Player");

 function Update () 
 {
  if(CanMove == true && firing == false)
  {
     var controller : CharacterController = GetComponent(CharacterController);
     if (controller.isGrounded) {
         // We are grounded, so recalculate
         // move direction directly from axes
         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                 Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= mspeed;

        if (Input.GetButton ("Jump") && (jumpTimer > 2) && (CanMove == true)) {  
        moveDirection.y = jumpSpeed;
        jumpTimer = 1.0;  
        }//end if jump input
     }
 
     // Apply gravity
     moveDirection.y -= gravity * Time.deltaTime;
     
     // Move the controller
     controller.Move(moveDirection * Time.deltaTime);
     
     }//end if
      
     
     if(CanMove == false)
     {
      moveTimer -= Time.deltaTime;
     }
     
     if (moveTimer <= 0.0)
     {
      if(HaveLives == true) //if player still has lives...
      //set player position back into the camera's viewing area.
      //Instantiate(Player,Vector3(0,-2.190212,4.103298), Quaternion.identity);
      transform.position.x = transform.position.x + 10.0; // <-- Beginning of level
  transform.position.y = -1.879804; // this just makes sure the character is "standing" on the platform beneath him
      CanMove = true; //set CanMove back to true, but only if HaveLives is true.
      moveTimer = 2.0; //reset move timer
     }
     jumpTimer += Time.deltaTime; //used to update a timer var for jumping
     
     
     if (firing == true)
     {
     transform.Translate(0,0,0,Space.World);
     
      
     }
 } //end Update

 @script RequireComponent(CharacterController)
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 Lorcan · Jul 07, 2012 at 04:07 PM 0
Share

Discovered what was causing this problem. $$anonymous$$y player character had a Box Collider, a character controller, and a rigidbody. After I disabled the Box Collider, the speed bug disappeared. I also modified the speed variable so that the player object didn't move real fast. I originally bumped up the speed variable in my player move script because with the box collider the player was moving incredibly slow.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to reduce speed while moving left and right in 2D platform 0 Answers

isGrounded is always false, even with gravity, how do you fix that? 1 Answer

How to set a max horizontal speed, 2D platformer 1 Answer

Collision update speed 0 Answers

Is mixing movement types a bad thing to do? (Force and Translate) 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