Hi,
I am sure this question gets asked a bunch, but I am trying to get some basic player movement implemented into my project. I know there are many tutorials and free code files, but I wanted to try and figure it out on my own since this is the first time I am doing it.
The main issue I am seeing right now is that the player seems to be able to briefly overlap the wall and also sinks into the floor a little. I have a Player GameObject with a RigidBody2D that is Dynamic and currently has Continuous CollisionDetection and Interpolate set to None. This GameObject has a child GameObject for checking if the Player is on the ground. Other than my script, the GroundCheck GameObject has a BoxCollider2D that has IsTrigger set to true and is positioned so that the top is the very bottom of the Player.
Vertically, I first start my game by setting my player to x = 0 y = -3 and immediately upon hitting the play button, the Player’s y value goes to -3.085, as if it’s sinking into the floor a little. For reference, my floor is a Tilemap with a TilemapCollider2D and has IsTrigger set to false.
Horizontally, I am updating the Player with the following
_rigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * SpeedModifier, _rigidbody.velocity.y);
I am assuming me setting the horizontal velocity manually is what is causing the clipping into the wall? If so, am I supposed to just use AddForce instead for horizontal movement as well? The clipping into the wall is causing my GroundChecker to not work as intended, I think. If I jump into a wall and fall along it while still trying to move into the wall, my current logic thinks the Player is still in the air even after landing, so I can never jump again. To “fix” this, what I did was just making the X size of the collider smaller, but then that seemed to lead into other problems where the player would seem to just get stuck on nothing on the floor sometimes.
For the sake of completion, this is what I do when the player jumps
_rigidbody.AddForce(Vector2.up * Mathf.Sqrt(-2 * JumpHeight * (Physics2D.gravity.y * _rigidbody.gravityScale)), ForceMode2D.Impulse);
_groundChecker.IsGrounded = false;
and the GroundChecker just does this, where the entire tilemap, floor and wall, is tagged with “Ground”
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ground")
{
IsGrounded = true;
}
}
Sorry if any of this was not clear and any assistance/suggestions are appreciated.
Thanks.