0
\$\begingroup\$

I kinda have a problem that my player is snapping onto the other surface while I wanna move a ramp up. I want to have it rotate smooth to the normals of the ramp. How can I change the code below to a smoother rotation function of the player?

Code:


void NormalsOfGround()
{
    //RAYCASTS THE NORMALS ON THE GROUND THAT THE PLAYER MOVES ALONG SURFACE
    Debug.DrawRay(rayCastNormal.transform.position, -transform.up, Color.blue, 2.5f);
    Ray ray = new Ray (transform.position, -transform.up);
    RaycastHit hit;
    if(Physics.Raycast(ray, out hit, 2.5f, GroundLayer) == true)
    {
        transform.rotation = Quaternion.FromToRotation (transform.up, hit.normal) * transform.rotation;
    }   
}
\$\endgroup\$
0
\$\begingroup\$

You can rotate by a controlled number of degrees each frame with:

Quaternion.RotateTowards(from, to, maxDegreesDelta)

So your code can look something like this:

if(Physics.Raycast(ray, out hit, 2.5f, GroundLayer)) {

    var targetRotation = Quaternion.FromToRotation (
                 transform.up,
                 hit.normal
             ) * transform.rotation;

    transform.rotation = Quaternion.RotateTowards(
                transform.rotation,
                targetRotation,
                degreePerSecondSpeed * Time.deltaTime
             );
} 
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "Kinda" suggest there are some in-game results you might like to see improved? Lay it on us and I or another user here can try to help. :) \$\endgroup\$ – DMGregory Feb 7 '18 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.