C# Custom Collision Script (solved)

Okay, to clarify some things, I absolutely hate using Rigidbody physics. My disliking of them is a result of bugs, as the built-in physics engine is not optimum for 2.5d dungeon crawlers, at least from my experiments. I’m trying to write a custom collision engine to work around these bugs, so that it will be much more suited for what I’m trying to accomplish.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Physics3D : MonoBehaviour {
	public bool gravEnabled;

	float vSpeed;

	public float gravity = 3.5f;


	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update(){
		// Enable or disable gravity
		GravityEnabled (true);
	}

	// Check if gravity is enabled 
	bool GravityEnabled(bool enabled){
		if (enabled == true)
			Gravity ();

		return enabled;
	}

	// Gravity function
	void Gravity(){
		transform.Translate(Vector3.down * vSpeed * gravity * Time.deltaTime, Space.World);

		if (Physics.Raycast(transform.position, Vector3.down, 1)) {
			vSpeed = 0;
		} else {
			vSpeed=1.5f;
		}
	}

    // This is where I need help
	void OnCollisionEnter(Collision other){
		foreach (ContactPoint c in other.contacts) {
			if (transform.position.z == c.thisCollider.transform.position.z - 0.5f) {
					// Block the direction
			} //else if(other collision stuff)
		}
	}
}

Now, this may be relatively simple, but simple is all I need. The question is, how can I have it correctly
block directions without disabling the entire axis? (If the object that was blocking was to the left, the player could not walk walk to the right in my previous version of this script)

If you’re looking to block directions (presumably when you walk into a wall, object, etc.), you may want to look into using a LayerMask within you Raycast, as they make it so the Ray will only return true if it hits an object on a certain layer.

For instance create a Layer called “Solid” (or something similar) and change your Raycast to this:

if (Physics.Raycast(transform.position, Vector3.down, 1, 1 << LayerMask.NameToLayer("YourLayerName"))) 
    {
         vSpeed = 0
    } 
    else 
    {
        vSpeed=1.5f;
    }

This should help you if I am understanding your issue correctly. Regardless, would love to know what works for you once you get it working.