I thought that if i set up rays on all four sides of the character and compared their distances to the ground to find out if standing on a slope and rotate the character incrementally frame by frame(to make the change seamless) until the rayhits had the same distance(therefore making the character seem to be standing on flat ground even on a wall or ceiling). Since i believe i set up my gravity to work locally, I believed it should work…here’s that part(and the gravity part) of code…
public float TiltIncrement = 0.01f;
void ProcessMotion()
{
//Tranform Vector to world space
MoveVector = transform.TransformDirection(MoveVector);
//Normalize vector if magnitude is > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
//apply slope
WallRun();
//apply sliding if able
// ApplySlide();
//apply speed
MoveVector *= MoveSpeed;
//Reapply VertVelocity to MoveVector.y
MoveVector = new Vector3(MoveVector.x, VertVelocity, MoveVector.z);
//apply gravity
ApplyGravity();
//Move Character in World Space
TP_Controller.CharController.Move(MoveVector * Time.deltaTime);
}
void ApplyGravity()
{
if (MoveVector.y > -TermVelocity)
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
if (TP_Controller.CharController.isGrounded MoveVector.y < -1)
MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
}
void WallRun()
{
RaycastHit Front;
RaycastHit Back;
RaycastHit Left;
RaycastHit Right;
Physics.Raycast(transform.position + new Vector3(0, 1, 1), Vector3.down, out Front);
Physics.Raycast(transform.position + new Vector3(0, 1, -1), Vector3.down, out Back);
Physics.Raycast(transform.position + new Vector3(-1, 1, 0), Vector3.down, out Left);
Physics.Raycast(transform.position + new Vector3(1, 1, 0), Vector3.down, out Right);
do
{
transform.Rotate(-Vector3.right * TiltIncrement);
} while (Front.distance < Back.distance);
do
{
transform.Rotate(Vector3.right * TiltIncrement);
} while (Front.distance > Back.distance);
do
{
transform.Rotate(-Vector3.forward * TiltIncrement);
} while (Left.distance > Right.distance);
do
{
transform.Rotate(Vector3.forward * TiltIncrement);
} while (Left.distance < Right.distance);
}
}
While I was putting it together, the logic seemed rock solid. Sadly, the instant I hit play…Unity froze completely.
Luckily, i had saved, but I really want to have this character ability. If anyone has a better idea of how to code this, I would really appreciate the assistance.