It looks like you're new here. If you want to get involved, click one of these buttons!
I wanted to make a 2D character controller but I ran into some problems . I don't know if all these are code problems, I am still learning unity. Here are the problems:
1.Character(which is a unity 2D square) phases into a wall a little when moving towards the wall
2.Player floats of the ground a little so I need to change the offset on the collider, I don't think this is the best solution for the problem
Here is the code
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { private Rigidbody2D body; private float movementX; private float speed = 7f; public float jumpForce = 200f; private bool isGrounded; [SerializeField] private LayerMask whatIsGround; private float groundedRadius = 0.4f; [SerializeField] private Transform groundCheck; private bool isFacingRight; private void Awake() { body = GetComponent<Rigidbody2D>(); } void Update() { Move(); GroundCheck(); Jump(); if(movementX < 0 && !isFacingRight) { Flip(); } else if(movementX > 0 && isFacingRight) { Flip(); } } private void Move() { movementX = Input.GetAxisRaw("Horizontal"); transform.position += new Vector3(movementX, 0f, 0f) * speed * Time.deltaTime; } private void Jump() { if(isGrounded && Input.GetButtonDown("Jump")) { isGrounded = false; body.AddForce(new Vector2(0f, jumpForce)); } } private void GroundCheck() { Collider2D collider = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround); if(collider != null) { isGrounded = true; } else isGrounded = false; } private void Flip() { isFacingRight = !isFacingRight; transform.Rotate(0f, 180f, 0f); } private void OnDrawGizmos() { Gizmos.DrawWireSphere(groundCheck.position, groundedRadius); } }