CharacterController2D.move C# (CSharp) Method

move() public method

attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to stop when run into.
public move ( Vector3 deltaMovement ) : void
deltaMovement Vector3 Delta movement.
return void
	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;

		var desiredPosition = transform.position + deltaMovement;
		primeRaycastOrigins( desiredPosition, deltaMovement );


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

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

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


		// move then update our state
		if( usePhysicsForMovement )
		{
			rigidbody2D.MovePosition( transform.position + deltaMovement );
			velocity = rigidbody2D.velocity;
		}
		else
		{
			transform.Translate( deltaMovement, Space.World );

			// only calculate velocity if we have a non-zero deltaTime
			if( Time.deltaTime > 0 )
				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] );
		}
	}

Usage Example

コード例 #1
0
ファイル: Villager.cs プロジェクト: sky-coding/ludum43
    void Update()
    {
        if (isDead)
        {
            return;
        }

        if (characterController.isGrounded)
        {
            _velocity.y = 0;
        }

        var smoothedMovementFactor = characterController.isGrounded ? groundDamping : inAirDamping;

        _velocity.x  = Mathf.Lerp(_velocity.x, _normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);
        _velocity.y += gravity * Time.deltaTime;
        characterController.move(_velocity * Time.deltaTime);
        _velocity = characterController.velocity;

        var levelBoundsMin = levelBoundsPolygonCollider2D.bounds.min;
        var levelBoundsMax = levelBoundsPolygonCollider2D.bounds.max;

        if (transform.position.x < levelBoundsMin.x || transform.position.x > levelBoundsMax.x ||
            transform.position.y < levelBoundsMin.y || transform.position.y > levelBoundsMax.y)
        {
            Destroy(gameObject);
        }
    }
All Usage Examples Of CharacterController2D::move