- Home /
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)
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.
Your answer
Follow this Question
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