I want to make a 3D platformer for a school project. Since I'm not that experienced in game development, I followed Plai's tutorial. My biggest problem are that I keep sliding down slopes and the slope detection. It detects the slope only if there's a surface below the slope, the player has some distance from the ground yet is not too far from the ground.
This is the method shown to detect the slope. Tried editing it but nothing seems to work.
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 * 5f))
{
if (slopeHit.normal != Vector3.up) return true;
}
Color rayColor;
if (slopeHit.collider != null) rayColor = Color.green;
else rayColor = Color.red;
Debug.DrawRay(transform.position, Vector3.down, rayColor);
return slopeHit.collider != null;
CodePudding user response:
well in the first part
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 * 5f))
{
if (slopeHit.normal != Vector3.up) return true;
}
if you hit something and it is a slope you return
already.
Otherwise you execute the rest even if you maybe wouldn't hit anything at all.
You would probably rather do e.g.
if (Physics.Raycast(transform.position, Vector3.down, out var slopeHit, playerHeight / 2f * 5f))
{
var isSlope = slopeHit.normal != Vector3.up;
Debug.DrawLine(transform.position, slopeHit.point, isSlope ? Color.yellow : Color.green);
return isSlope;
}
//meaning you don't hit anything at all
Debug.DrawRay(transform.position, Vector3.down, Color.red);
return false;