问题
I'm developing a platformer in Unity using the 2D engine. I have my player character which has a BoxCollider2D and a RigidBody, and a number of 'walls' which have BoxColliders.
Now, I copied the script for moving the player from another project and made some changes. The part which has to do with movement is as follows:
public void FixedUpdate()
{
physVel = Vector2.zero;
// move left
if(Input.GetKey(KeyCode.LeftArrow))
{
physVel.x = -runVel;
}
// move right
if(Input.GetKey(KeyCode.RightArrow))
{
physVel.x = runVel;
}
// jump
if(Input.GetKey(KeyCode.UpArrow))
{
if(jumps < maxJumps)
{
jumps += 1;
if(jumps == 1)
{
_rigidbody.velocity = new Vector2(physVel.x, jumpVel);
}
}
}
//Apply gravity
_rigidbody.AddForce(-Vector3.up * fallVel);
// actually move the player
_rigidbody.velocity = new Vector2(physVel.x, _rigidbody.velocity.y);
}
Now this works perfectly fine.
The problem arises if the player jumps into a wall. If I keep the direction button mashed 'towards' the wall after having jumped, he is suspended in mid-air. As in the collision appears to be reducing movement on both axis to zero. If I release the direction, he falls normally. Collisions on the other axis works fine. I can hit my head or walk without issue.
Am I missing something obvious?
回答1:
EDIT: try adding a material with 0 friction to both player and walls and see what happens, if it stops it is a friction error.
来源:https://stackoverflow.com/questions/25042690/collision-on-x-axis-means-0-movement-in-y-axis