コード例 #1
0
ファイル: Player.cs プロジェクト: dgamepuzzle/sokoban-unity
    public void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.gameObject.tag == "Crate")
        {
            Crate crate = hit.gameObject.GetComponent <Crate> ();

            float xDirection = hit.moveDirection.x;
            float zDirection = hit.moveDirection.z;

            if (xDirection == 1.0f || xDirection == -1.0f)
            {
                zDirection = 0.0f;
            }
            else if (zDirection == 1.0f || zDirection == -1.0f)
            {
                xDirection = 0.0f;
            }
            else
            {
                xDirection = 0.0f;
                zDirection = 0.0f;
            }

            Vector3 direction = new Vector3(xDirection, 0.0f, zDirection);
            crate.Push(direction);
        }
    }
コード例 #2
0
    /// <summary>
    /// Check if the player is available to move
    /// </summary>
    /// <returns>true if yes, false if not</returns>
    bool CanMove()
    {
        Vector3 direction = Vector3.zero;

        switch (movingDirection)
        {
        case 1:
            direction = Vector3.up;
            break;

        case 2:
            direction = Vector3.right;
            break;

        case 3:
            direction = Vector3.down;
            break;

        case 4:
            direction = Vector3.left;
            break;
        }

        RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 1.28f, 1);

        if (hit)
        {
            Debug.DrawLine(new Vector3(transform.position.x, transform.position.y, 0), hit.point, Color.red);
            if (hit.transform.tag == wallsTag)//If raycast hit something and it's a wall
            {
                return(false);
            }
            else
            if (hit.transform.tag == cratesTag)   //if hit a crate, we now verify if that crate can move
            {
                Crate theCrate = hit.transform.GetComponent <Crate>();
                if (!theCrate.CanMove(direction))
                {
                    return(false);
                }
                else
                {
                    theCrate.Push(movingDirection, speed);
                }
            }
        }
        return(true);
    }