1
votes

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.

  1. 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.
  2. 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.

1
I'm not familiour with Unity, but can you not calculate the angle between the normal vector and some reference vector that represents a flat ground. The angle will be in radians. You can convert this to degrees. The dot product doesn't give you the angle exactly. You should find the formula on Wikipedia. I would imagine that Unity already has a method for calculating the angle though.Homar

1 Answers

1
votes

Calculating angles is simple Trigonometry.

Use:

  angle  = arctan (y/x)

You can use Math.f to from Unity to help you out, it returns floats instead of double unlike .Net Math.

Radians to degree

    Angle * 180/pi