I would like to know what the current angle the player is walking on. I have Googled and got this to work
turn = fAntToCamRotate;
curDir = (curDir + turn) % 360; // rotate angle modulo 360 according to input
RaycastHit hit;
if (Physics.Raycast(target.transform.position, -curNormal, out hit))
{
curNormal = Vector3.Lerp(curNormal, hit.normal, 4 * Time.deltaTime);
Quaternion grndTilt = Quaternion.FromToRotation(Vector3.up, curNormal);
//GetComponent<CapsuleCollider>().transform.rotation = grndTilt * Quaternion.Euler(0, curDir, 0);
target.transform.rotation = grndTilt * Quaternion.Euler(0, curDir, 0);
/*
* FIX THIS CODE TO KNOW THE ANGEL THE PLAYER IS WALKING ON!!!
Vector3 test = Vector3.up;
Vector3 test2 = hit.normal;
float tester = Vector3.Dot(test.normalized, test2.normalized);
Debug.Log(tester * (180 / Mathf.PI));
*/
}
With this code the player follows the ground, so it no longer looks like they are jumping small distances when running down a slope. However, I got two problems with this that I'm hoping someone could help me with.
- As you see in the out commented code I have tried to get the angle the player currently is walking on and save it in a variable. My problem with this is that I get this really weird values and I have no clue to why. If my player goes from a flat surface to a 25 degree leaning slope the value changes from flat (about 55) to angle (about 51). Which I don't get at all. Does anyone know how to make this into degrees that are more understandable? The reason I want to save this is so I can tell if there is a really steep slope I want the player to glide.
- My second problem is that I have an empty gameobject with a rigidbody and collision box as my parent for my character. The camera is a child. I would like the body to turn according to the slope so I no longer jump down, but I want the camera to not follow the player angle, is there a way to fix this?
For my second question, I think I could solve it myself after playing some more with it. But if anyone could help me with the first question I would love that, I have tried for hours without luck, and I have no idea why the angle gives me so weird numbers.