Home Game Development unity – Rigidbody motion course on slope not matching with participant orientation

unity – Rigidbody motion course on slope not matching with participant orientation

0
unity – Rigidbody motion course on slope not matching with participant orientation

[ad_1]

I’ve encountered a difficulty with my Rigidbody motion script on sloped surfaces. When shifting up and down small slopes, the motion works as anticipated, however when shifting up and down steep slopes I discover that the course of the motion does not fairly line up with the place the digital camera is dealing with.

Example (Red Ray represents Rigidbody motion vector, Object represents Rigidbody rotation):
Red Ray represents Rigidbody movement vector, Object is represents Rigidbody rotation

I’m utilizing the next code to perform my slope motion, the vector param is simply the specified course of motion:

Vector3 GetVectorOnSlope(Vector3 vector) {
    Ray ray = new Ray(orientationTransform.place, Vector3.down);
    if (Physics.Raycast(ray, out RaycastHit hitInfo, 1f, groundLayer)) {
        Vector3 cross = Vector3.Cross(Vector3.up, hitInfo.regular);
        float angle = Vector3.SignedAngle(Vector3.up, hitInfo.regular, cross);
        Quaternion rotation = Quaternion.AngleAxis(angle, cross);
        Vector3 consequence = rotation * vector;
        Debug.DrawRay(remodel.place, consequence * 2f, Color.crimson, 5f);
        return consequence;
    }
    return vector;
}

I’ve tried a number of options like scrapping this strategy and simply sticking the participant to the slope, utilizing ProjectOnPlane, nonetheless I’ve had little success and this appears to be a extra elegant strategy so I’d wish to determine this one out.

Any assistance is significantly appreciated!

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here