What you are looking for is the Dot product.
You want to disable movement when the player is inside a wall, so
if(Vector3.Dot(velocity,hit.normal) < -0.1) velocity = Vector3.zero; //when you get better replace this with a custom-made collision function, which will nullify the component of the velocity towards the wall
It would be easier for you to use one raycast in the direction you are moving.
In order to do this you will have to use transform.forward for the direction of the raycast. If you really want to create a capable-of-working-in-any-situation collision system your knowledge of Vector3’s should be impeccable (you should eventually know the concepts of the Dot Product, Cross Product, Vector Projection, Vector Rejection, Vector Reflection).
For said raycast, look into this code will help:
var hit : RaycastHit;
var hit2 : RaycastHit;
if(Physics.Raycast(pos - Vector3.up*0.5,dir,hit,Mathf.Infinity,(1 << 0))
&& Physics.Raycast(pos - Vector3.up*0.5,-hit.normal,hit2,Mathf.Infinity, (1 << 0))
&& (hit.distance < radius + dist || hit2.distance < radius + dist))//find the normal below us and find how close it is
{
if(hit.distance > hit2.distance)
{
hit.distance = hit2.distance - radius;
}
else hit.distance -= radius;
}
return hit;
(I call this IterationArray.)
It basically finds the first normal in the direction the direction of the velocity, then takes that normal and finds how close it is. You should optimize this even more by using some logic like this:
if(Physics.Check Capsule) //if there is something occupying the space, then do stuff, otherwise don't bother
{
if(IterationArray) //check if we are on a flat plane first
else if(Physics.SphereCast) //check for a contour or corner of a mesh
}
I’m currently making a parkour package that I’ll put up on the Unity Asset Server for free, so when I get there, I’ll try to inform you. The parkour package has a pre-collision system with a failsafe of the Unity3D collision system that prevents bad things from happening. In the OnCollisionStay field, a Debug.Log() is rarely called, meaning that the precollisions work most of the time (but definitely not all of the time).