Beispiel #1
0
        void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            if (enabled)
            {
                CharacterControllerComponentData componentData = new CharacterControllerComponentData
                {
                    Gravity              = Gravity,
                    MovementSpeed        = MovementSpeed,
                    MaxMovementSpeed     = MaxMovementSpeed,
                    RotationSpeed        = RotationSpeed,
                    JumpUpwardsSpeed     = JumpUpwardsSpeed,
                    MaxSlope             = math.radians(MaxSlope),
                    MaxIterations        = MaxIterations,
                    CharacterMass        = CharacterMass,
                    SkinWidth            = SkinWidth,
                    ContactTolerance     = ContactTolerance,
                    AffectsPhysicsBodies = AffectsPhysicsBodies,
                };
                CharacterControllerInternalComponentData internalData = new CharacterControllerInternalComponentData
                {
                    Entity = entity,
                    Input  = new CharacterControllerInputComponentData(),
                };

                dstManager.AddComponentData(entity, componentData);
                dstManager.AddComponentData(entity, internalData);
            }
        }
            private void HandleUserInput(CharacterControllerComponentData ccComponentData, float3 up, float3 surfaceVelocity,
                                         ref CharacterControllerInternalComponentData ccInternalData, ref float3 linearVelocity)
            {
                // Reset jumping state and unsupported velocity
                if (ccInternalData.SupportedState == CharacterSupportStateEnum.Supported)
                {
                    ccInternalData.IsJumping           = false;
                    ccInternalData.UnsupportedVelocity = float3.zero;
                }

                // Movement and jumping
                bool   shouldJump = false;
                float3 requestedMovementDirection = float3.zero;
                {
                    float3 forward = math.forward(quaternion.identity);
                    float3 right   = math.cross(up, forward);

                    float horizontal    = ccInternalData.Input.Move.x;
                    float vertical      = ccInternalData.Input.Move.y;
                    bool  jumpRequested = ccInternalData.Input.Jump > 0;
                    bool  haveInput     = (math.abs(horizontal) > float.Epsilon) || (math.abs(vertical) > float.Epsilon);
                    if (haveInput)
                    {
                        float3 localSpaceMovement = forward * vertical + right * horizontal;
                        //float3 worldSpaceMovement = math.rotate(quaternion.AxisAngle(up, ccInternalData.CurrentRotationAngle), localSpaceMovement);
                        //requestedMovementDirection = math.normalize(worldSpaceMovement);
                        requestedMovementDirection = localSpaceMovement;
                    }
                    shouldJump = jumpRequested && ccInternalData.SupportedState == CharacterSupportStateEnum.Supported;
                }

                // Turning
                {
                    //float horizontal = ccInternalData.Input.Aim.x;
                    //bool haveInput = (math.abs(horizontal) > float.Epsilon);
                    //if (haveInput)
                    //{
                    //    ccInternalData.CurrentRotationAngle += horizontal * ccComponentData.RotationSpeed * DeltaTime;
                    //}

                    bool haveInput = (math.abs(ccInternalData.Input.Aim.x) > float.Epsilon) ||
                                     (math.abs(ccInternalData.Input.Aim.y) > float.Epsilon);

                    if (haveInput)
                    {
                        float3 currentForward = math.forward(quaternion.AxisAngle(up, ccInternalData.CurrentRotationAngle));
                        float3 desiredForward = new float3(ccInternalData.Input.Aim.x, 0.0f, ccInternalData.Input.Aim.y);

                        ccInternalData.CurrentRotationAngle += Utilities.RadianAngleSigned(currentForward, desiredForward) * ccComponentData.RotationSpeed * DeltaTime;
                    }
                    else
                    {
                        haveInput = (math.abs(ccInternalData.Input.Move.x) > float.Epsilon) ||
                                    (math.abs(ccInternalData.Input.Move.y) > float.Epsilon);

                        if (haveInput)
                        {
                            float3 currentForward = math.forward(quaternion.AxisAngle(up, ccInternalData.CurrentRotationAngle));
                            float3 desiredForward = new float3(ccInternalData.Input.Move.x, 0.0f, ccInternalData.Input.Move.y);

                            ccInternalData.CurrentRotationAngle += Utilities.RadianAngleSigned(currentForward, desiredForward) * ccComponentData.RotationSpeed * DeltaTime;
                        }
                    }

                    ccInternalData.CurrentRotationAngle = Utilities.RadianClampAngle(ccInternalData.CurrentRotationAngle);
                }

                // Apply input velocities
                {
                    if (shouldJump)
                    {
                        // Add jump speed to surface velocity and make character unsupported
                        ccInternalData.IsJumping           = true;
                        ccInternalData.SupportedState      = CharacterSupportStateEnum.Unsupported;
                        ccInternalData.UnsupportedVelocity = surfaceVelocity + ccComponentData.JumpUpwardsSpeed * up;
                    }
                    else if (ccInternalData.SupportedState != CharacterSupportStateEnum.Supported)
                    {
                        // Apply gravity
                        ccInternalData.UnsupportedVelocity += ccComponentData.Gravity * DeltaTime;
                    }
                    // If unsupported then keep jump and surface momentum
                    linearVelocity = requestedMovementDirection * ccComponentData.MovementSpeed +
                                     (ccInternalData.SupportedState != CharacterSupportStateEnum.Supported ? ccInternalData.UnsupportedVelocity : float3.zero);
                }
            }