FlaxEngine icon indicating copy to clipboard operation
FlaxEngine copied to clipboard

[Feature Request] Make CharacterController expose boolean "isSliding" to play sounds/animations when it happens.

Open buzzdx opened this issue 10 months ago • 2 comments

When using the character controller you can set it to prevent climbing and force sliding down slopes bigger than a threshold.

It would be very useful to be able to get informed about this state of sliding. Right now I don't know how to, for example, deny the player the ability to jump while sliding. Without denying it, the player can circumvent the slide and just keep jumping to get higher.

Another thing this would allow for is playing a sliding sound, or letting the character play a sliding animation.

buzzdx avatar Apr 27 '24 17:04 buzzdx

I'm not sure if PhysX lib we use for physics simulation provides such information.

mafiesto4 avatar Apr 29 '24 16:04 mafiesto4

If physx doesn't allow for that, could you implement it yourself?

I used this code to implement the sliding behaviour myself (following a youtube guide that is ^^)

`private void HandleSlopeSlidingCapsuleCast() {
// length needed to hit the ground even when on very steep surface - might need to change if it's not working correctly everyhwere float tracelength = (75f/2f)+10f;

        if(Physics.CapsuleCast(Actor.Position, 75f, 0f, Vector3.Down, out RayCastHit hit, Quaternion.Euler(Vector3.Zero), tracelength, TerrainCollisionLayer, false))
        {                
            //Debug.Log("Ground detected!");
            //BoundingSphere b;
            //b.Center = hit.Point;
            //b.Radius = 5;
            //DebugDraw.DrawSphere(b, Color.Red, 1f);

            float angle = Vector3.Angle(hit.Normal, Vector3.Up);
            //Debug.Log("Angle of ground: " + angle);                

            if(angle > 45f)
            {
                //Debug.Log("We should slide now!");
                slopeSlideVelocity = Vector3.ProjectOnPlane(new Vector3(0, yVelocity, 0), hit.Normal);                    

                // NOTE: i was looking for a way to make the sliding go faster. just multiplying the velocity 
                //       seems to do the trick just fine ^^
                // UPDATE: multiplaying the Y component led to the char seemingly jumping higher. removing it
                //         got me to the desired effect.
                slopeSlideVelocity.X *= 2;
                slopeSlideVelocity.Z *= 2;
                return;
            }
        }

        // fade out sliding instead of instant stop as long as we have a certain speed. deactivate this part to get instant stop.
        // NOTE: however, this will still result in the char being pushed away from slope he walked onto. 
        if(isSliding)
        {
            slopeSlideVelocity -= slopeSlideVelocity * Time.DeltaTime * 12;
            
            if(slopeSlideVelocity.Length > 50)
            {                    
                return;
            }
            else
            {
                // need this here to one-time after sliding re-enable player input
                MovementProhibited = false;
            }
        }

        if(slopeSlideVelocity != Vector3.Zero)
        {
            slopeSlideVelocity = Vector3.Zero;
            MovementProhibited = false;
        }
    }`
    

It will remove player control and then slide down a slope that is detected beneath the player. The problem with this is, as you can see from the NOTE in there, if the player just walks onto a slope that would result in sliding, he first walks onto it, then gets pushed back. I think this is because this movement i manually add clashes with the normal movement of the engine?

When set to prevent climbing + force sliding without my custom code, the character would just not step on that slope to begin with. Would it be possible to implement this kind of code on the engine level to prevent that pushback?

Also kind of weird that PhysX would not expose that information, but I don't know much about it so this i just me wondering how other engines with physx do that kind of thing then ^^

buzzdx avatar Apr 30 '24 12:04 buzzdx