- Home /
Question by
BrandonIsDrippy · Dec 15, 2021 at 09:34 PM ·
collisionmovementboxcolliderclipping
Player character clipping through box collider
I'm a beginner working on a simple 2.5D gravity platformer similar to VVVVV. my problem is that whenever my player picks up too much momentum it starts to clip through the floor/ceiling colliders. here's a quick video:
https://www.youtube.com/watch?v=MNh_vt5qf78
code sample here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerScript : MonoBehaviour
{
// variables
[SerializeField] private LayerMask playerMask;
public float speed = 5.0f;
public float gravityForce = 50.0f;
public bool gravityButton = true;
public bool isOnGround = false;
float horizonalMovement;
private Rigidbody rigidBodyComponent;
// Start is called before the first frame update
void Start()
{
rigidBodyComponent = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isOnGround) {
if (gravityButton) {
gravityButton = false;
}
else {
gravityButton = true;
}
}
horizonalMovement = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * Time.deltaTime * speed * horizonalMovement);
}
private void FixedUpdate() {
if (gravityButton) {
Physics.gravity = new Vector3(0, -gravityForce, 0);
}
else {
Physics.gravity = new Vector3(0, gravityForce, 0);
}
}
private void OnCollisionEnter(Collision collision) {
if (collision.gameObject.CompareTag("Ground")) {
isOnGround = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
GameObject B does not collide with GameObject A 1 Answer
Player Clipping into Wall on collision,Player Clipping into Walls 2 Answers
Sphere overlaps vertical box collider and stutters 1 Answer
Box collider (with Rigidbody attached) gets stuck into another Box Collider 0 Answers
Player jumping from wall sometimes jump even when not collided with other wall 0 Answers