I’m trying to make a game where you control two characters, both of which can only collide with the “universal” ground and their respective color. (For example, the green character can only collide with green ground and white ground while the red character can only collide with red ground and white ground.) I’m currently trying to disable green’s collision with red ground while still keeping the red ground’s collider enabled. I’ve got something that works, but there’s a split second delay between touching the red platform and being able to pass through it, so green hesitates a bit before passing through it. If anyone could help me figure out how to remove that delay, that would be great. Attached is my player movement script and the script that disables collision for red blocks.
Also, there’s a bug where even though collision is disabled for the red platform, it still allows you to jump while touching its hitbox. Help fixing this would be appreciated.
Player Movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float horizontal;
public float speed = 8f;
public float jumpingPower = 16f;
private bool isFacingRight = true;
private float coyoteTime = 0.2f;
private float coyoteTimeCounter;
private float jumpBufferTime = 0.2f;
private float jumpBufferCounter;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
if (IsGrounded())
{
coyoteTimeCounter = coyoteTime;
}
else
{
coyoteTimeCounter -= Time.deltaTime;
}
if (Input.GetButtonDown("Jump"))
{
jumpBufferCounter = jumpBufferTime;
}
else
{
jumpBufferCounter -= Time.deltaTime;
}
if (jumpBufferCounter > 0f && coyoteTimeCounter > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
jumpBufferCounter = 0f;
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
coyoteTimeCounter = 0f;
}
Flip();
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private void FixedUpdate()
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
}
Phase Through Red:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhaseThroughRed : MonoBehaviour
{
private GameObject currentRedPlatform;
[SerializeField] private BoxCollider2D playerCollider;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (currentRedPlatform != null)
{
DisableCollision();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Red"))
{
currentRedPlatform = collision.gameObject;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Red"))
{
currentRedPlatform = null;
}
}
private void DisableCollision()
{
BoxCollider2D redCollider = currentRedPlatform.GetComponent<BoxCollider2D>();
Physics2D.IgnoreCollision(playerCollider, redCollider);
}
}
