It doesnt clip throught but when i hold A or D it just buggs in and out
Does anyone know what to do?
I would suggest showing your code for your movement as well. Use code tags.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float MovementSpeed = 1f;
public float JumpForce = 1f;
bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
bool isTouchingFront;
public Transform frontCheck;
bool wallSliding;
public float wallSlidingSpeed;
bool wallJumping;
public float xWallForce;
public float yWallForce;
public float wallJumpTime;
public Rigidbody2D rb;
private int moveInput;
private void Start()
{
rb = GetComponent();
}
private void Update()
{
var movement = Input.GetAxis(“Horizontal”);
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
if (Input.GetButtonDown(“Jump”) && Mathf.Abs(rb.velocity.y) < 0.001f)
{
rb.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
isTouchingFront = Physics2D.OverlapCircle(frontCheck.position, checkRadius, whatIsGround);
if (isTouchingFront == true && isGrounded == false && moveInput != 0)
{
wallSliding = true;
} else
{
wallSliding = false;
}
if (wallSliding)
{
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
if (Input.GetKeyDown(KeyCode.UpArrow) && wallSliding == true)
{
wallJumping = true;
Invoke(“setWallJumpingToFalse”, wallJumpTime);
}
if (wallJumping == true)
{
rb.velocity = new Vector2(xWallForce * -moveInput, yWallForce);
}
void setWallJumpingToFalse()
{
wallJumping = false;
}
}
}
You are moving your object via the Transform component, which ignores physics.
If you want your movement to respect physics, such as not clipping into walls, you must either use Raycasts to detect the walls or use a Rigidbody and its methods to move the object.