Harry_C
Harry_C

Reputation: 177

Problem with my ground tiles and player movement Unity 2D

I have problem with my player movement on the ground tiles. This is my very first trials with Unity 2D. I am trying to build a simple 2D plattformer.Suddenly the horizontal ground goes vertical and start to roll around [Suddenly the horizontal ground goes vertical and start to roll aroundthis is my tilemap Collider 2D

I think the problem is with the Tilemap Collider 2D but I am not really sure. Here are the scripts I am currently using for the player movement:

public class playerMovement2D : MonoBehaviour {

protected Rigidbody2D body;
protected Vector2 velocity;

public float jumpVelocity = 20.0f;

float horizontal;
float vertical;
bool isGrounded = false;
public Transform isGroundedChecker;
public float checkGroundRadius;
public LayerMask groundLayer;
public float runSpeed = 10.0f;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;



// Start is called before the first frame update
void Start()
{
    body = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
    Move();
    Jump();
    BetterJump();
    CheckIfGrounded();
   
}

private void Move()
{
    horizontal = Input.GetAxisRaw("Horizontal");
    float moveBy = horizontal * runSpeed;
    body.velocity = new Vector2(moveBy, body.velocity.y);

}

private void Jump()
{
    if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    {
        body.velocity = new Vector2(jumpVelocity, body.velocity.x);
    }
}

void CheckIfGrounded()
{
    Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
    if (collider != null)
    {
        isGrounded = true;
    }
    else
    {
        isGrounded = false;
    }
}

void BetterJump()
{
    if (body.velocity.y < 0)
    {
        body.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
    }
    else if (body.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
    {
        body.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
    }
}

}

Upvotes: 0

Views: 721

Answers (1)

Xauyho
Xauyho

Reputation: 16

Have you frozen the rotation in the Rigidbody 2D component? It’s listed under constraints.

Upvotes: 0

Related Questions