Beispiel #1
0
    protected override void FixedUpdate()
    {
        base.FixedUpdate();
        if (!IsServer && !IsOwnerClient)
        {
            return;
        }

        var velocity = CacheRigidbody.velocity;

        if (CurrentHp > 0)
        {
            var moveDirectionMagnitude = moveDirection.magnitude;
            if (!IsPlayingActionAnimation() && moveDirectionMagnitude != 0)
            {
                if (moveDirectionMagnitude > 1)
                {
                    moveDirection = moveDirection.normalized;
                }

                var targetVelocity = moveDirection * CacheMoveSpeed;

                // Apply a force that attempts to reach our target velocity
                Vector3 velocityChange = (targetVelocity - velocity);
                velocityChange.x = Mathf.Clamp(velocityChange.x, -CacheMoveSpeed, CacheMoveSpeed);
                velocityChange.y = 0;
                velocityChange.z = Mathf.Clamp(velocityChange.z, -CacheMoveSpeed, CacheMoveSpeed);
                CacheRigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
                // Calculate rotation on client only, will send update to server later
                CacheTransform.rotation = Quaternion.RotateTowards(CacheTransform.rotation, Quaternion.LookRotation(moveDirection), angularSpeed * Time.fixedDeltaTime);
            }

            BaseCharacterEntity tempCharacterEntity;
            if (moveDirectionMagnitude == 0 && TryGetTargetEntity(out tempCharacterEntity))
            {
                var targetDirection = (tempCharacterEntity.CacheTransform.position - CacheTransform.position).normalized;
                if (targetDirection.magnitude != 0f)
                {
                    var fromRotation   = CacheTransform.rotation.eulerAngles;
                    var lookAtRotation = Quaternion.LookRotation(targetDirection).eulerAngles;
                    lookAtRotation          = new Vector3(fromRotation.x, lookAtRotation.y, fromRotation.z);
                    CacheTransform.rotation = Quaternion.RotateTowards(CacheTransform.rotation, Quaternion.Euler(lookAtRotation), angularSpeed * Time.fixedDeltaTime);
                }
            }
            // Jump
            if (isGrounded && isJumping)
            {
                CacheRigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
                isJumping = false;
            }
        }

        if (Mathf.Abs(velocity.y) > groundingDistance)
        {
            isGrounded = false;
        }

        // We apply gravity manually for more tuning control
        CacheRigidbody.AddForce(new Vector3(0, Physics.gravity.y * CacheRigidbody.mass * gravityRate, 0));
    }
Beispiel #2
0
        protected override void EntityFixedUpdate()
        {
            base.EntityFixedUpdate();
            Profiler.BeginSample("PlayerCharacterEntity - FixedUpdate");

            if (movementSecure == MovementSecure.ServerAuthoritative && !IsServer)
            {
                return;
            }

            if (movementSecure == MovementSecure.NotSecure && !IsOwnerClient)
            {
                return;
            }

            tempMoveDirection = Vector3.zero;

            if (HasNavPaths)
            {
                tempTargetPosition    = navPaths.Peek();
                tempTargetPosition.y  = 0;
                tempCurrentPosition   = CacheTransform.position;
                tempCurrentPosition.y = 0;
                tempMoveDirection     = (tempTargetPosition - tempCurrentPosition).normalized;
                if (Vector3.Distance(tempTargetPosition, tempCurrentPosition) < StoppingDistance)
                {
                    navPaths.Dequeue();
                    if (!HasNavPaths)
                    {
                        StopMove();
                    }
                }
            }

            tempPreviousVelocity = CacheRigidbody.velocity;
            if (!IsDead())
            {
                // If move by WASD keys, set move direction to input direction
                if (tempInputDirection.magnitude != 0f)
                {
                    tempMoveDirection = tempInputDirection;
                }

                tempMoveDirectionMagnitude = tempMoveDirection.magnitude;
                if (!IsPlayingActionAnimation() && tempMoveDirectionMagnitude != 0f)
                {
                    if (tempMoveDirectionMagnitude > 1)
                    {
                        tempMoveDirection = tempMoveDirection.normalized;
                    }

                    tempTargetVelocity = tempMoveDirection * GameInstance.GameplayRule.GetMoveSpeed(this);

                    // Apply a force that attempts to reach our target velocity
                    Vector3 velocityChange = (tempTargetVelocity - tempPreviousVelocity);
                    velocityChange.x = Mathf.Clamp(velocityChange.x, -CacheMoveSpeed, CacheMoveSpeed);
                    velocityChange.y = 0;
                    velocityChange.z = Mathf.Clamp(velocityChange.z, -CacheMoveSpeed, CacheMoveSpeed);
                    CacheRigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
                    tempLookAtRotation      = Quaternion.RotateTowards(CacheTransform.rotation, Quaternion.LookRotation(tempMoveDirection), angularSpeed * Time.fixedDeltaTime);
                    tempLookAtRotation.x    = 0;
                    tempLookAtRotation.z    = 0;
                    CacheTransform.rotation = tempLookAtRotation;
                }
                else
                {
                    // Stop movement
                    CacheRigidbody.velocity = new Vector3(0, CacheRigidbody.velocity.y, 0);
                }

                BaseGameEntity tempEntity;
                if (tempMoveDirectionMagnitude == 0f && TryGetTargetEntity(out tempEntity))
                {
                    tempTargetDirection = (tempEntity.CacheTransform.position - CacheTransform.position).normalized;
                    if (tempTargetDirection.magnitude != 0f)
                    {
                        tempLookAtRotation      = Quaternion.RotateTowards(CacheTransform.rotation, Quaternion.LookRotation(tempTargetDirection), angularSpeed * Time.fixedDeltaTime);
                        tempLookAtRotation.x    = 0f;
                        tempLookAtRotation.z    = 0f;
                        CacheTransform.rotation = tempLookAtRotation;
                    }
                }
                // Jump
                if (isGrounded && isJumping)
                {
                    RequestTriggerJump();
                    CacheRigidbody.velocity = new Vector3(tempPreviousVelocity.x, CalculateJumpVerticalSpeed(), tempPreviousVelocity.z);
                    isJumping = false;
                }
            }

            if (Mathf.Abs(tempPreviousVelocity.y) > groundingDistance)
            {
                isGrounded = false;
            }

            // We apply gravity manually for more tuning control
            CacheRigidbody.AddForce(new Vector3(0, Physics.gravity.y * CacheRigidbody.mass * gravityRate, 0));
            Profiler.EndSample();
        }
Beispiel #3
0
        private void UpdateMovement()
        {
            GroundCheck();

            // If move by WASD keys, set move direction to input direction
            if (tempInputDirection.magnitude > 0f)
            {
                tempMoveDirection = tempInputDirection;
            }

            if (IsDead())
            {
                tempMoveDirection = Vector3.zero;
                IsJumping         = false;
            }

            if (tempMoveDirection.magnitude > 0f)
            {
                tempMoveDirection = tempMoveDirection.normalized;

                // always move along the camera forward as it is the direction that it being aimed at
                tempMoveDirection = Vector3.ProjectOnPlane(tempMoveDirection, groundContactNormal).normalized;

                float currentTargetSpeed = gameInstance.GameplayRule.GetMoveSpeed(this);
                // If character move backward
                if (Vector3.Angle(tempMoveDirection, CacheTransform.forward) > 120)
                {
                    currentTargetSpeed *= backwardMoveSpeedRate;
                }

                tempMoveDirection *= currentTargetSpeed;
                if (IsGrounded)
                {
                    CacheRigidbody.velocity = tempMoveDirection;
                }
                else
                {
                    CacheRigidbody.velocity = new Vector3(tempMoveDirection.x, CacheRigidbody.velocity.y, tempMoveDirection.z);
                }
            }
            else
            {
                CacheRigidbody.velocity = new Vector3(0f, CacheRigidbody.velocity.y, 0f);
            }

            if (IsGrounded)
            {
                CacheRigidbody.drag = 5f;

                if (IsJumping)
                {
                    RequestTriggerJump();
                    CacheRigidbody.drag     = 0f;
                    CacheRigidbody.velocity = new Vector3(CacheRigidbody.velocity.x, 0f, CacheRigidbody.velocity.z);
                    CacheRigidbody.AddForce(new Vector3(0f, CalculateJumpVerticalSpeed(), 0f), ForceMode.Impulse);
                    applyingJump = true;
                    IsGrounded   = false;
                }

                if (!applyingJump &&
                    Mathf.Abs(tempMoveDirection.x) < float.Epsilon &&
                    Mathf.Abs(tempMoveDirection.z) < float.Epsilon &&
                    CacheRigidbody.velocity.magnitude < 1f)
                {
                    CacheRigidbody.Sleep();
                }
            }
            else
            {
                CacheRigidbody.drag = 0f;
                if (previouslyGrounded && !applyingJump)
                {
                    StickToGroundHelper();
                }
            }
            IsJumping = false;
        }