2D Platformer - Sinking into colliders & bumping over tilemap
Hey fellow Unity Developers, hope you're having a nice day!
I'm currently developing a 2D Platformer and i have this issue with my player sinking into colliders and also hitting bumps when moving over an platform made up from a tileset.
So my question is, how is the best way to do a 2D Platformer Controller, and handle collisions - i'm still fairly new to Unity and C# so would love to learn about new ways to achieve this.
Here is my PlayerController:
using System.Collections;
using System.Collections.Generic;
using Com.LuisPedroFonseca.ProCamera2D;
using UnityEngine;
public class PlayerController : MonoBehaviour {
[Header("Player Movement")]
public LayerMask playerMask;
public float maxSpeed = 7f;
public float jumpTakeOffSpeed = 7f;
public bool canMoveInAir = true;
public Transform groundCheck;
public bool grounded;
private float playerInput;
private Vector2 velocity;
private Rigidbody2D rb2d;
private float groundCheckRadius = 0.2f;
void OnEnable()
{
rb2d = GetComponent<Rigidbody2D>();
ProCamera2D.Instance.AddCameraTarget(transform);
}
void Start () {
}
void FixedUpdate()
{
velocity = rb2d.velocity;
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, playerMask);
Debug.DrawLine(transform.position, groundCheck.position, Color.red);
Move(Input.GetAxis("Horizontal"));
if (Input.GetButton("Jump"))
{
Jump();
}
}
public void Move(float horizontalInput)
{
if (!canMoveInAir && !grounded)
return;
velocity.x = horizontalInput * maxSpeed;
rb2d.velocity = velocity;
}
public void Jump()
{
if (grounded && velocity.y == 0)
{
rb2d.AddForce(Vector2.up * jumpTakeOffSpeed, ForceMode2D.Impulse);
}
}
public void Die()
{
Destroy(gameObject);
}
}
Answer by ismaelnascimento01 · May 28, 2017 at 12:56 PM
Use Collider 2D and Rigibody2D
I have attached my code, you can see that i'm already using a Rigidbody2D and just to confirm i am using only 2D colliders.
check if option 'is Trigger' have active in collisors
Amount of gravity on my Player is set to 3
Check link: https://docs.unity3d.com/$$anonymous$$anual/class-SpriteRenderer.html And the size of the collider is according to the sprite
Your answer

Follow this Question
Related Questions
How To Do Basic 2D Movement? 1 Answer
why does my 2d platformer lags? 1 Answer
Help with a roll effect 2D 0 Answers
If player hits certain X value in opposition to Camera, shift camera question. 0 Answers
Unity52D - Player Jump Bug 0 Answers