Beispiel #1
0
        public virtual void ControlSpeed(float velocity)
        {
            if (Time.deltaTime == 0)
            {
                return;
            }

            this.velocity = velocity;

            if (useRootMotion)
            {
                Vector3 v = (animator.deltaPosition * (velocity > 0 ? velocity : 1f)) / Time.deltaTime;
                v.y = rigidbody.velocity.y;
                rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, v, 20f * Time.deltaTime);
            }
            else
            {
                Vector3 velY = transform.forward * velocity * Mathf.Clamp(speed, -1, 1);
                velY.y = rigidbody.velocity.y;
                Vector3 velX = transform.right * velocity * Mathf.Clamp(direction, -1, 1);
                velX.x = rigidbody.velocity.x;

                if (isStrafing)
                {
                    Vector3 v = (transform.TransformDirection(new Vector3(TPCInput.GetAxis("Horizontal"), 0, TPCInput.GetAxis("Vertical"))) * (velocity > 0 ? velocity : 1f));
                    v.y = rigidbody.velocity.y;
                    rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, v, 20f * Time.deltaTime);
                }
                else
                {
                    rigidbody.velocity = velY;
                    rigidbody.AddForce(transform.forward * (velocity * Mathf.Clamp(speed, -1, 1)) * Time.deltaTime, ForceMode.VelocityChange);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Change distance between camera and character by scrolling mouse wheel
        /// </summary>
        protected virtual void ScrollingCameraDistance()
        {
            if (!scrollCameraDistance)
            {
                return;
            }

            float mouseWheelAxis   = TPCInput.GetAxis("Mouse Wheel") * scrollSensitivity;
            float scrolledDistance = defaultDistance + (mouseWheelAxis * Time.deltaTime);

            defaultDistance = Mathf.Clamp(scrolledDistance, minDistance, maxDistance);
        }
Beispiel #3
0
        /// <summary>
        /// Update the targetDirection variable using referenceTransform or just input.Rotate by word  the referenceDirection
        /// </summary>
        /// <param name="referenceTransform"></param>
        public virtual void UpdateTargetDirection(Transform referenceTransform = null)
        {
            if (referenceTransform)
            {
                Vector3 forward = keepDirection ? referenceTransform.forward : referenceTransform.TransformDirection(Vector3.forward);
                forward.y = 0;

                forward   = keepDirection ? forward : referenceTransform.TransformDirection(Vector3.forward);
                forward.y = 0;                 //set to 0 because of referenceTransform rotation on the X axis

                //get the right-facing direction of the referenceTransform
                Vector3 right = keepDirection ? referenceTransform.right : referenceTransform.TransformDirection(Vector3.right);

                // determine the direction the player will face based on input and the referenceTransform's right and forward directions
                targetDirection = TPCInput.GetAxis("Horizontal") * right + TPCInput.GetAxis("Vertical") * forward;
            }
            else
            {
                targetDirection = keepDirection ? targetDirection : new Vector3(TPCInput.GetAxis("Horizontal"), 0, TPCInput.GetAxis("Vertical"));
            }
        }
Beispiel #4
0
        protected virtual void CameraHandle()
        {
            float vertical   = TPCInput.GetAxis("Mouse X");
            float horizontal = TPCInput.GetAxis("Mouse Y");

            RotateCamera(vertical, horizontal);

            if (characterMotor.LockMovement)
            {
                return;
            }
            // tranform Character direction from camera if not KeepDirection
            if (!characterMotor.KeepDirection)
            {
                characterMotor.UpdateTargetDirection(transform);
            }
            // rotate the character with the camera while strafing
            if (characterMotor.IsStrafing && characterMotor.MoveAmount > 0)
            {
                characterMotor.RotateWithAnotherTransform(transform);
            }
        }
Beispiel #5
0
        private bool StepOffset()
        {
            if (Mathf.Sqrt(MoveAmount) < 0.1 || !isGrounded)
            {
                return(false);
            }

            RaycastHit _hit = new RaycastHit();
            Vector3    _movementDirection = isStrafing && MoveAmount > 0 ? (transform.right * TPCInput.GetAxis("Horizontal") + transform.forward * TPCInput.GetAxis("Vertical")).normalized : transform.forward;
            Ray        rayStep            = new Ray((transform.position + new Vector3(0, stepOffsetEnd, 0) + _movementDirection * ((capsuleCollider).radius + 0.05f)), Vector3.down);

            if (Physics.Raycast(rayStep, out _hit, stepOffsetEnd - stepOffsetStart, groundLayer) && !_hit.collider.isTrigger)
            {
                if (_hit.point.y >= (transform.position.y) && _hit.point.y <= (transform.position.y + stepOffsetEnd))
                {
                    float   _speed            = isStrafing ? Mathf.Clamp(MoveAmount, 0, 1) : Mathf.Clamp(speed, -1, 1);
                    Vector3 velocityDirection = isStrafing ? (_hit.point - transform.position) : (_hit.point - transform.position).normalized;
                    rigidbody.velocity = velocityDirection * stepSmooth * (_speed * (velocity > 1 ? velocity : 1));
                    return(true);
                }
            }
            return(false);
        }
Beispiel #6
0
 public virtual void StrafeMovement()
 {
     speed     = Mathf.Clamp(TPCInput.GetAxis("Vertical"), -1, 1);
     direction = Mathf.Clamp(TPCInput.GetAxis("Horizontal"), -1, 1);
 }