The question is answered, right answer was accepted
Vector3.Distance returning offset values due to down force interference.
Essentially this is what I am doing 1.Casting a Ray down to the ground from player. 2.float distance = Vector3.Distance(transform.position, hit.point);
When it executes, before I input anything the values seem reasonable. Once I input movement the values vary sporadically up to 9.0 when it shouldnt exceed more than 2 at most. I have encountered the problem but have no solution. Essentially the character controller down force is interfering with the Distance values because there is a verticalVelocity += downforce value; or to be more specific movement.y -= speed.
Before you say, duh make sure your player is grounded and dont add force when it is. THAT was one problem to begin with. The character controller isGrounded check returns true/false several times per second if there is now downforce thus immediately causing problems with jump if you try jumping while isGrounded = true. To add to this pain, if youre on a slope and moving down, the character controller bounces downward causing even more sporadic checks of the is grounded bool. So I have fixed all of these pains by default by adding constant forces to Y value to ensure I am grounded at ALL times when my feet are planted. By default, try it, Debug.Log a character controller's isGrounded (CharacterController.isGrounded) bool without force. It will consistently return true and false. I cant seem to find a solution because at no point can I turn those forces off or my controller will not be responsive. All is grounded check return perfectly fine as is with constant forces. I also print(myposition + hit.point) and they both are the expected static values even while I move. So why cant vector3.Distance keep track of that???? Anyways I will post my code and hopefully someone will have luck.
Lastly you may ask, why do I need this, 90% of the controller is functional, especially for what I specifically need. Slopes work fine say up to 80 degrees. After that, I have several issues where too steep is too much and the raycast doesnt pick up the floor and I can climb steep slopes with no sliding. So i want to check that raycast distance. If the control isGrounded and the distance is great, then I am floating on a steep slope and I use the OnControllerColliderHit to solve that. By default OnControllerColloderHit is terrible with smaller slopes, but with steep it work great so I want to use a combination of the two. Thank you.
Answer by Gabestronaut · May 16, 2017 at 09:34 PM
UPDATE: It turns the weird number fluctuations were epsilon values. In order to solve the numbers:
float distance = Mathf.Round(hit.distance * 100) / 100; Debug.Log(distance);
So I was getting the right distance, but it was just giving me Epsilon Values or the precision float closes to 0 but not 0. So that was throwing me off big time. I was expecting something close to 0 or 0. NOT
9.########e##
Follow this Question
Related Questions
i am using a code to make my cube walk with character controller ,but when i play the cube spins. 0 Answers
Character Controller Move function Explanation 0 Answers
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers
Using Velocity breaks my jumping 0 Answers