Question by
BobbaPB · Nov 18, 2020 at 03:51 PM ·
scripting problembugbeginner
Rigidbody player gets stuck on walls in Unity 3D project.
Hello
I have a Unity 3D project which involves a blob, and some obstacles to interact with. I have set up so my player can move around and jump, but the problem is that when I jump into an obstacles side, my player gets frozen in mid-air until you either press the jump button again, or until you let go of the movement key. I tried applying a no friction physics material to my player, but that caused my movement to totally bug out when jumping into a wall. Does anyone have any advice? Here is the code
public class PlayerController : MonoBehaviour
{
public float speed = 5;
private Rigidbody rb;
public LayerMask groundLayers;
public float jumpForce = 4;
public CapsuleCollider col;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
col = GetComponent<CapsuleCollider>();
}
// Update is called once per frame
void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical).normalized * speed * Time.deltaTime;
rb.MovePosition(transform.position + movement);
if (isGrounded() && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private bool isGrounded()
{
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.radius * .9f, groundLayers);
}
}
Comment