Beispiel #1
0
        /// <summary>
        /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
        /// stop when run into.
        /// </summary>
        /// <param name="deltaMovement">Delta movement.</param>
        public void move(Vector3 deltaMovement)
        {
            // save off our current grounded state which we will use for wasGroundedLastFrame and becameGroundedThisFrame
            collisionState.wasGroundedLastFrame = collisionState.below;

            // clear our state
            collisionState.Reset();
            _raycastHitsThisFrame.Clear();
            primeRaycastOrigins();

            // now we check movement in the horizontal dir
            if (deltaMovement.x != 0f)
            {
                moveHorizontally(ref deltaMovement);
            }

            // next, check movement in the vertical dir
            if (deltaMovement.y != 0f)
            {
                moveVertically(ref deltaMovement);
            }

            // move then update our state
            deltaMovement.z = 0;
            if (transform != null)
            {
                transform.Translate(deltaMovement, Space.World);
            }

            // only calculate velocity if we have a non-zero deltaTime
            if (Time.deltaTime > 0f)
            {
                velocity = deltaMovement / Time.deltaTime;
            }

            // set our becameGrounded state based on the previous and current collision state
            if (!collisionState.wasGroundedLastFrame && collisionState.below)
            {
                collisionState.becameGroundedThisFrame = true;
            }

            // send off the collision events if we have a listener
            if (onControllerCollidedEvent != null)
            {
                for (var i = 0; i < _raycastHitsThisFrame.Count; i++)
                {
                    onControllerCollidedEvent(_raycastHitsThisFrame[i]);
                }
            }

            ignoreOneWayPlatformsThisFrame = false;
        }
    public void Move(Vector2 deltaMovement)
    {
        collisionState.wasGroundedLastFrame = collisionState.below;

        collisionState.Reset();
        raycastHitsThisFrame.Clear();
        isGoindUpSlope = false;

        PrimeRaycastOrigins();

        if (deltaMovement.y < 0f && collisionState.wasGroundedLastFrame)
        {
            HandleVerticalSlope(ref deltaMovement);
        }

        if (deltaMovement.x != 0f)
        {
            MoveHorizontally(ref deltaMovement);
        }

        if (deltaMovement.y != 0f)
        {
            MoveVertically(ref deltaMovement);
        }

        transform.Translate(deltaMovement, Space.World);

        if (Time.deltaTime > 0f)
        {
            velocity = deltaMovement / Time.deltaTime;
        }

        if (!collisionState.wasGroundedLastFrame && collisionState.below)
        {
            collisionState.becameGroundedThisFrame = true;
        }

        if (isGoindUpSlope)
        {
            velocity.y = 0;
        }

        if (onControllerCollidedEvent != null)
        {
            for (int i = 0; i < raycastHitsThisFrame.Count; i++)
            {
                onControllerCollidedEvent(raycastHitsThisFrame [i]);
            }
        }

        ignoreOneWayPlatformsThisFrame = false;
    }
    /// <summary>
    /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
    /// stop when run into.
    /// </summary>
    /// <param name="deltaMovement">Delta movement.</param>
    public void Move(Vector3 deltaMovement)
    {
        // clear our state
        collisionState.Reset();
        _raycastHitsThisFrame.Clear();

        PrimeRaycastOrigins();

        // now we check movement in the horizontal dir
        if (deltaMovement.x != 0f)
        {
            MoveHorizontally(ref deltaMovement);
        }

        // next, check movement in the vertical dir
        if (deltaMovement.y != 0f)
        {
            MoveVertically(ref deltaMovement);
        }

        // move then update our state
        deltaMovement.z = 0;
        transform.Translate(deltaMovement, Space.World);

        // only calculate velocity if we have a non-zero deltaTime
        if (Time.deltaTime > 0f)
        {
            velocity = deltaMovement / Time.deltaTime;
        }

        // send off the collision events if we have a listener
        if (OnControllerCollidedEvent != null)
        {
            for (var i = 0; i < _raycastHitsThisFrame.Count; i++)
            {
                OnControllerCollidedEvent(_raycastHitsThisFrame [i]);
            }
        }
    }
Beispiel #4
0
    /// <summary>
    /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
    /// stop when run into.
    /// </summary>
    /// <param name="deltaMovement">Delta movement.</param>
    public void Move(Vector3 deltaMovement)
    {
        // save off our current grounded state which we will use for wasGroundedLastFrame and becameGroundedThisFrame
        collisionState.wasGroundedLastFrame = collisionState.below;

        // clear our state
        collisionState.Reset();
        raycastHitsThisFrame.Clear();
        isGoingUpSlope = false;

        PrimeRaycastOrigins();

        // first, we check for a slope below us before moving
        // only check slopes if we are going down and grounded
        if (deltaMovement.y < 0f && collisionState.wasGroundedLastFrame)
        {
            HandleVerticalSlope(ref deltaMovement);
        }

        // now we check movement in the horizontal dir
        if (deltaMovement.x != 0f)
        {
            MoveHorizontally(ref deltaMovement);
        }

        // next, check movement in the vertical dir
        if (deltaMovement.y != 0f)
        {
            MoveVertically(ref deltaMovement);
        }

        // move then update our state
        deltaMovement.z = 0;
        transform.Translate(deltaMovement, Space.World);

        // only calculate velocity if we have a non-zero deltaTime
        if (Time.deltaTime > 0f)
        {
            velocity = deltaMovement / Time.deltaTime;
        }

        // set our becameGrounded state based on the previous and current collision state
        if (!collisionState.wasGroundedLastFrame && collisionState.below)
        {
            collisionState.becameGroundedThisFrame = true;
        }

        // if we are going up a slope we artificially set a y velocity so we need to zero it out here
        if (isGoingUpSlope)
        {
            velocity.y = 0;
        }

        // send off the collision events if we have a listener
        if (OnControllerCollidedEvent != null)
        {
            for (var i = 0; i < raycastHitsThisFrame.Count; i++)
            {
                OnControllerCollidedEvent(raycastHitsThisFrame[i]);
            }
        }

        ignoreOneWayPlatformsThisFrame = false;
    }
        /// <summary>
        /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
        /// stop when run into.
        /// </summary>
        /// <param name="deltaMovement">Delta movement.</param>
        public void Move(float deltaTime, Vector3 deltaMovement, bool isJumpingThisFrame = false)
        {
            Vector3 originalDelta = deltaMovement;

            // save off our current grounded state which we will use for wasGroundedLastFrame and becameGroundedThisFrame
            collisionState.wasGroundedLastFrame = collisionState.below;

            // clear our state
            collisionState.Reset();
            raycastHitsThisFrame.Clear();
            isGoingUpSlope = false;

            PrimeRaycastOrigins();

            // only check slopes if we are going down and grounded
            if (deltaMovement.y < 0f && collisionState.wasGroundedLastFrame)
            {
                HandleVerticalSlope(ref deltaMovement);
            }

            // now we check movement in the horizontal dir
            if (deltaMovement.x != 0f)
            {
                MoveHorizontally(ref deltaMovement, isJumpingThisFrame);
            }

            // next, check movement in the vertical dir
            if (deltaMovement.y != 0f)
            {
                MoveVertically(ref deltaMovement);
            }

            // move then update our state
            deltaMovement.z = 0;
            transform.Translate(deltaMovement, Space.World);

            // only calculate velocity if we have a non-zero deltaTime
            if (deltaTime > 0f)
            {
                // Keep the original X delta
                // This is primarily to ensure we don't become ungrounded when turning around on slopes
                velocity = new Vector3(originalDelta.x, deltaMovement.y, 0f) / deltaTime;
            }

            // set our becameGrounded state based on the previous and current collision state
            if (!collisionState.wasGroundedLastFrame && collisionState.below)
            {
                collisionState.becameGroundedThisFrame = true;
            }

            // if we are going up a slope we artificially set a y velocity so we need to zero it out here
            if (isGoingUpSlope)
            {
                velocity.y = 0;
            }

            // send off the collision events if we have a listener
            if (OnControllerCollidedEvent != null)
            {
                for (int i = 0; i < raycastHitsThisFrame.Count; i++)
                {
                    OnControllerCollidedEvent(raycastHitsThisFrame[i]);
                }
            }

            ignoreOneWayPlatformsThisFrame = false;
        }
Beispiel #6
0
        /// <summary>
        /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
        /// stop when run into.
        /// </summary>
        /// <param name="deltaMovement">Delta movement.</param>
        public void Move(Vector2 deltaMovement, Vector2 input, bool standingOnPlatform = false)
        {
            PrimeRaycastOrigins();

            // save off our current grounded state which we will use for wasGroundedLastFrame and becameGroundedThisFrame
            collisionState.wasGroundedLastFrame = collisionState.below;
            // clear our state
            collisionState.Reset();
            _raycastHitsThisFrame.Clear();
            _isGoingUpSlope = false;
            playerInput     = input;


            // first, we check for a slope below us before moving
            // only check slopes if we are going down and grounded
            if (deltaMovement.y < 0f && collisionState.wasGroundedLastFrame)
            {
                DecendSlope(ref deltaMovement);
            }

            if (deltaMovement.x != 0)
            {
                collisionState.faceDir = (int)Mathf.Sign(deltaMovement.x);
            }

            CheckHorizontalCollision(ref deltaMovement);

            if (deltaMovement.y != 0)
            {
                CheckVerticalCollision(ref deltaMovement);
            }

            transform.Translate(deltaMovement, Space.World);      //actual movement happens here!

            // 计算速度
            if (Time.deltaTime > 0f)
            {
                velocity = deltaMovement / Time.deltaTime;
            }

            //如果移动前不在地面上,而移动后到达了地面,则把如下变量设为true
            if (!collisionState.wasGroundedLastFrame && collisionState.below)
            {
                collisionState.becameGroundedThisFrame = true;
            }

            // if we are going up a slope we artificially set a y velocity so we need to zero it out here
            if (_isGoingUpSlope)
            {
                velocity.y = 0;
            }

            // 委托事件调用
            if (onControllerCollidedEvent != null)
            {
                for (var i = 0; i < _raycastHitsThisFrame.Count; i++)
                {
                    onControllerCollidedEvent(_raycastHitsThisFrame[i]);
                }
            }

            if (standingOnPlatform && !collisionState.wasGroundedLastFrame)
            {
                collisionState.below = true;
                collisionState.becameGroundedThisFrame = true;
            }

            ignoreOneWayPlatformsThisFrame = false;
        }
        /// <summary>
        /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
        /// stop when run into.
        /// </summary>
        /// <param name="deltaMovement">Delta movement.</param>
        private Vector3 MoveBy(Vector3 velocity, Vector3 deltaMovement)
        {
            // save off our current grounded state which we will use for wasGroundedLastFrame and becameGroundedThisFrame
            _collisionState.WasGroundedLastFrame = _collisionState.Below;

            // clear our state
            _collisionState.Reset();
            _raycastHitsThisFrame.Clear();
            _isGoingUpSlope = false;

            PrimeRaycastOrigins();

            // first, we check for a slope below us before moving
            // only check slopes if we are going down and grounded
            if (deltaMovement.y < 0f && _collisionState.WasGroundedLastFrame)
            {
                HandleVerticalSlope(ref deltaMovement);
            }

            // now we check movement in the horizontal dir
            if (deltaMovement.x != 0f)
            {
                MoveHorizontally(ref deltaMovement);
            }

            // next, check movement in the vertical dir
            if (deltaMovement.y != 0f)
            {
                MoveVertically(ref deltaMovement);
            }

            // move then update our state
            deltaMovement.z = 0;
            transform.Translate(deltaMovement, Space.World);

            // only calculate velocity if we have a non-zero deltaTime
            if (Time.deltaTime > 0f)
            {
                velocity = deltaMovement / Time.deltaTime;
            }

            // set our becameGrounded state based on the previous and current collision state
            if (!_collisionState.WasGroundedLastFrame && _collisionState.Below)
            {
                _collisionState.BecameGroundedThisFrame = true;
                _startedFalling = 0.0f;
            }

            //Check if we started falling this frame
            if (_collisionState.WasGroundedLastFrame && !_collisionState.Below)
            {
                _startedFalling = Time.time;
            }

            // if we are going up a slope we artificially set a y velocity so we need to zero it out here
            if (_isGoingUpSlope)
            {
                velocity.y = 0;
            }

            // send off the collision events if we have a listener
            if (Events.OnControllerCollidedEvent != null)
            {
                for (int i = 0; i < _raycastHitsThisFrame.Count; i++)
                {
                    Events.OnControllerCollidedEvent(_raycastHitsThisFrame[i]);
                }
            }

            IgnoreOneWayPlatformsThisFrame = false;

            return(velocity);
        }