// Update is called once per frame
 void Update()
 {
     if (!usingButtons)
     {
         float x = Input.GetAxisRaw("Horizontal");
         if (x == 0)
         {
             moveDirection = MOVE_DIR.STOP;
         }
         else
         {
             if (x < 0)
             {
                 moveDirection = MOVE_DIR.LEFT;
             }
             else
             {
                 moveDirection = MOVE_DIR.RIGHT;
             }
         }
         if (Input.GetKeyDown("space"))
         {
             Shot();
         }
     }
 }
 // Logic here to update movementState when a key is PRESSED DOWN
 // The most recent key press should override whatever the current
 // movementState is.

 private void HandleKeyDown()
 {
     if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         movementState = MOVE_DIR.UP;
     }
     

     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         
        {
             
 movementState = MOVE_DIR.DOWN; 

         }
     }
     

     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         
        {
             
 movementState = MOVE_DIR.LEFT; 

         }
     }
     

     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         
        {
             
 movementState = MOVE_DIR.RIGHT; 

         }
     }
 }
Beispiel #3
0
 public void ResetDir()
 {
     if (moveDir != MOVE_DIR.NONE)
     {
         moveDir = MOVE_DIR.NONE;
     }
 }
Beispiel #4
0
    // Update is called once per frame
    void Update()
    {
        // 指定した線分内に、ブロックレイヤーがあるかどうかを判定し、
        // ジャンプが可能かどうかを調べる
        canJump =
            Physics2D.Linecast(transform.position - (transform.right * 0.3f), transform.position - (transform.up * 0.1f), blockLayer) ||
            Physics2D.Linecast(transform.position + (transform.right * 0.3f), transform.position - (transform.up * 0.1f), blockLayer);

        // キーボード操作時
        if (!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");
            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }

            if (Input.GetKeyDown("space"))
            {
                PushJumpButton();
            }
        }
    }
Beispiel #5
0
    //获取方向 单位向量
    public static Vector3 GetDirVec(MOVE_DIR dir)
    {
        if (MOVE_DIR.NONE == 0)
        {
        }
        switch (dir)
        {
        case MOVE_DIR.UP:
            return(Vector3.forward);

        case MOVE_DIR.DOWN:
            return(Vector3.back);

        case MOVE_DIR.LEFT:
            return(Vector3.left);

        case MOVE_DIR.RIGHT:
            return(Vector3.right);

        case MOVE_DIR.UP_LEFT:
            return(VecUpLeft);

        case MOVE_DIR.UP_RIGHT:
            return(VecUpRight);

        case MOVE_DIR.DOWN_LEFT:
            return(VecDownLeft);

        case MOVE_DIR.DOWN_RIGHT:
            return(VecDownRight);
        }
        return(Vector3.zero);
    }
Beispiel #6
0
    // Update is called once per frame
    void Update()
    {
        if (!usingButtons)           //キーボードでの移動
        {
            float x = Input.GetAxisRaw("Horizontal");
            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }
        }

        if (jumpCount < MAX_JUMP_COUNT && Input.GetKeyDown("c"))
        {
            isJump = true;
            audioSource.PlayOneShot(jumpSE);
        }
    }
Beispiel #7
0
    private void FixedUpdate()
    {
        bool isBlock;

        switch (moveDirection)
        {
        case MOVE_DIR.LEFT:
            rbody.velocity       = new Vector2(moveSpeed * -1, rbody.velocity.y);
            transform.localScale = new Vector2(1, 1);
            isBlock = Physics2D.Linecast(
                new Vector2(transform.position.x, transform.position.y + 0.5f),
                new Vector2(transform.position.x - 0.3f, transform.position.y + 0.5f),
                blockLayer);

            if (isBlock)
            {
                moveDirection = MOVE_DIR.RIGHT;
            }
            break;

        case MOVE_DIR.RIGHT:
            rbody.velocity       = new Vector2(moveSpeed, rbody.velocity.y);
            transform.localScale = new Vector2(-1, 1);
            isBlock = Physics2D.Linecast(
                new Vector2(transform.position.x, transform.position.y + 0.5f),
                new Vector2(transform.position.x + 0.3f, transform.position.y + 0.5f),
                blockLayer);

            if (isBlock)
            {
                moveDirection = MOVE_DIR.LEFT;
            }
            break;
        }
    }
Beispiel #8
0
    void Update()
    {
        canJump =
            Physics2D.Linecast(transform.position - (transform.right * 0.3f), transform.position - (transform.up * 0.1f), blockLayer) ||
            Physics2D.Linecast(transform.position + (transform.right * 0.3f), transform.position - (transform.up * 0.1f), blockLayer);

        if (!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");

            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }
            if (Input.GetButton("Jump"))
            {
                PushJumpButton();
            }
        }
        //Animatorへパラメーターを送る
        anim.SetFloat("velY", rbody.velocity.y);
        anim.SetBool("isGrounded", canJump);
    }
Beispiel #9
0
    // Update is called once per frame
    void Update()
    {
        canJump = Physics2D.Linecast(transform.position - (transform.right * 0.1f),
                                     transform.position - (transform.up * 0.1f), blockLayer) ||
                  Physics2D.Linecast(transform.position + (transform.right * 0.1f),
                                     transform.position - (transform.up * 0.1f), blockLayer);

        //何も押してないとき
        moveDirection = MOVE_DIR.STOP;

        //右方向キー
        if (Input.GetKey(KeyCode.RightArrow))
        {
            moveDirection = MOVE_DIR.RIGHT;
        }

        //左方向キー
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            moveDirection = MOVE_DIR.LEFT;
        }

        //上方向キー
        if (Input.GetKey(KeyCode.UpArrow))
        {
            if (canJump)
            {
                goJump = true;
            }
        }
    }
Beispiel #10
0
    private void FixedUpdate()
    {
        switch (moveDirection)
        {
        case MOVE_DIR.STOP:
            moveSpeed = 0;
            break;

        case MOVE_DIR.LEFT:
            moveSpeed            = MOVE_SPEED * -1;
            transform.localScale = new Vector2(-1, 1);
            break;

        case MOVE_DIR.RIGHT:
            moveSpeed            = MOVE_SPEED;
            transform.localScale = new Vector2(1, 1);
            break;
        }

        rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);

        if (goJump)
        {
            audioSource.PlayOneShot(jumpSE);
            rbody.AddForce(Vector2.up * jumpPower);
            goJump = false;
        }

        if (!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");

            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }

            if (Input.GetKeyDown("space"))
            {
                PushJumpButton();
            }
        }
    }
Beispiel #11
0
    //发送操作指令
    private void SendMoveCommond(MOVE_DIR dir)
    {
        uint battleId = ZTBattleSceneManager.GetInstance().MyPlayer.BattleId;

        if (_moveDir != dir && battleId > 0 && ZTBattleSceneManager.GetInstance().MyPlayer.MoveDir != dir)
        {
            _moveDir = dir;
            BattleProtocol.GetInstance().SendMoveComand(battleId, dir);
            //MoveCommand command = FightDefine.GetMoveCommand(battleId, dir);
            //SceneEvent.GetInstance().dispatchEvent(SCENE_EVENT.ADD_COMMAND, new Notification(command));
        }
    }
Beispiel #12
0
    // 敵の移動処理
    void FixedUpdate()
    {
        bool isBlock;       // 進行方向にブロックがあるか否か

        switch (moveDirection)
        {
        case MOVE_DIR.LEFT:         // 左に移動
            // 左に移動するためRigidBodyにvelocityを設定(x軸は-1、y軸はそのまま)
            rbody.velocity = new Vector2(moveSpeed * -1, rbody.velocity.y);
            // 左移動の画像の向き(x,yともにそのまま)
            transform.localScale = new Vector2(1, 1);

            // ブロック衝突判定をLinecastにて行う、transform.positionにてキャラの位置座標を取得
            isBlock = Physics2D.Linecast(
                // Linecastのstart点、y軸はキャラのcirclecolliderの半径が0.25なので、円の一番上を指定
                new Vector2(transform.position.x, transform.position.y + 0.5f),
                // Linecastのend点、サークルの円より0.3先(左向きx軸マイナス)つまり鼻先を指定
                new Vector2(transform.position.x - 0.3f, transform.position.y + 0.5f),
                // LayerMaskにはblockLayerを指定し、Blockのみ判定
                blockLayer
                );
            // 進行方向にブロックがある
            if (isBlock)
            {
                // 右を向く
                moveDirection = MOVE_DIR.RIGHT;
            }
            break;

        case MOVE_DIR.RIGHT:        // 右に移動中
            // 右に移動するためRigidBodyにvelocityを設定(x、y軸はそのまま)
            rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);
            // 右移動の画像の向き(xは反転,yそのまま)
            transform.localScale = new Vector2(-1, 1);

            // ブロック衝突判定をLinecastにて行う、transform.positionにてキャラの位置座標を取得
            isBlock = Physics2D.Linecast(
                // Linecastのstart点、y軸はキャラのcirclecolliderの半径が0.25なので、円の一番上を指定
                new Vector2(transform.position.x, transform.position.y + 0.5f),
                // Linecastのend点、サークルの円より0.3先(右向きx軸プラス)
                new Vector2(transform.position.x + 0.3f, transform.position.y + 0.5f),
                // LayerMaskにはblockLayerを指定し、Blockのみ判定
                blockLayer
                );
            // 進行方向にブロックがある
            if (isBlock)
            {
                // 左を向く
                moveDirection = MOVE_DIR.LEFT;
            }
            break;
        }
    }
Beispiel #13
0
 public void PressRight(bool _pressed)
 {
     if (_pressed)
     {
         moveDir = MOVE_DIR.RIGHT;
     }
     else
     {
         moveDir = MOVE_DIR.NONE;
     }
     pressed = _pressed;
 }
Beispiel #14
0
 public void PressLeft(bool _pressed)
 {
     if (_pressed)
     {
         moveDir = MOVE_DIR.LEFT;
     }
     else
     {
         moveDir = MOVE_DIR.NONE;
     }
     pressed = _pressed;
 }
Beispiel #15
0
 public void PressDown(bool _pressed)
 {
     if (_pressed)
     {
         moveDir = MOVE_DIR.DOWN;
     }
     else
     {
         moveDir = MOVE_DIR.NONE;
     }
     pressed = _pressed;
 }
Beispiel #16
0
 public void PressUp(bool _pressed)
 {
     if (_pressed)
     {
         moveDir = MOVE_DIR.UP;
     }
     else
     {
         moveDir = MOVE_DIR.NONE;
     }
     pressed = _pressed;
 }
Beispiel #17
0
    public static List <Vector3> GetDirMoveVecs(MOVE_DIR dir)
    {
        List <Vector3> list = new List <Vector3>();

        switch (dir)
        {
        case MOVE_DIR.UP:
            list.Add(Vector3.forward);
            list.Add(Vector3.right);
            break;

        case MOVE_DIR.DOWN:
            list.Add(Vector3.back);
            list.Add(Vector3.left);
            break;

        case MOVE_DIR.LEFT:
            list.Add(Vector3.left);
            list.Add(Vector3.forward);
            break;

        case MOVE_DIR.RIGHT:
            list.Add(Vector3.right);
            list.Add(Vector3.back);
            break;

        case MOVE_DIR.UP_LEFT:
            list.Add(VecUpLeft);
            list.Add(Vector3.forward);
            list.Add(Vector3.left);
            break;

        case MOVE_DIR.UP_RIGHT:
            list.Add(VecUpRight);
            list.Add(Vector3.forward);
            list.Add(Vector3.right);
            break;

        case MOVE_DIR.DOWN_LEFT:
            list.Add(VecDownLeft);
            list.Add(Vector3.back);
            list.Add(Vector3.left);
            break;

        case MOVE_DIR.DOWN_RIGHT:
            list.Add(VecDownRight);
            list.Add(Vector3.back);
            list.Add(Vector3.right);
            break;
        }
        return(list);
    }
Beispiel #18
0
        public void MoveByControlPanel(MOVE_DIR dir)
        {
            switch (dir)
            {
            case MOVE_DIR.UP: AttemptMove <Enemy>(0, 1); break;

            case MOVE_DIR.DOWN: AttemptMove <Enemy>(0, -1); break;

            case MOVE_DIR.LEFT: AttemptMove <Enemy>(-1, 0); break;

            case MOVE_DIR.RIGHT: AttemptMove <Enemy>(1, 0); break;
            }
        }
    // This function takes a direction and handles Transforming the player
    // object in the appropriate direction

    private void Move(MOVE_DIR direction)
    {
        
            switch (direction)
        {
            

            case MOVE_DIR.IDLE: 

            break; 




        case MOVE_DIR.UP: 
                if (Input.GetKey(KeyCode.UpArrow) && (t.position == location))
            {
                
 
                {
                    
 location += Vector3.up; 

                }
            }
            
                break;

            

            case MOVE_DIR.DOWN: 
                if (Input.GetKey(KeyCode.DownArrow) && (t.position == location))
            {
                

                {
                    location += Vector3.down;
                }
            }
            break;


        case MOVE_DIR.LEFT:
            if (Input.GetKey(KeyCode.LeftArrow) && (t.position == location))
            {
                
                {
                    
 location += Vector3.left; 

                }
            }
            break; 



        case MOVE_DIR.RIGHT:
            if (Input.GetKey(KeyCode.RightArrow) && (t.position == location))
            {
                
 
                {
                    
 location += Vector3.right; 

                }
            }
            break;
        }

        transform.position = Vector3.MoveTowards(transform.position, location, Time.deltaTime * speed);
    }
Beispiel #20
0
    // Update is called once per frame
    void Update()
    {
        canJump =
            Physics2D.Linecast(trans.position - (trans.right * 0.3f),
                               trans.position - (trans.up * 0.1f), blockLayer) ||
            Physics2D.Linecast(trans.position + (trans.right * 0.3f),
                               trans.position - (trans.up * 0.1f), blockLayer);

        /*
         * Physics2D.Linecast(transform.position - (transform.right * 0.3f),
         *  transform.position - (transform.up * 0.1f), blockLayer) ||
         * Physics2D.Linecast(transform.position + (transform.right * 0.3f),
         *  transform.position - (transform.up * 0.1f), blockLayer);
         */

        //Debug.DrawLine(transform.position - (transform.right * 0.3f), transform.position - (transform.up * 0.1f), Color.red);
        //Debug.DrawLine(transform.position + (transform.right * 0.3f), transform.position - (transform.up * 0.1f), Color.red);

        if (!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");

            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }

            if (Input.GetKeyDown("space"))
            {
                //Debug.Log("Called");
                PushJumpButton();
            }
        }
    }
    void FixedUpdate()
    {
        // 進行方向にブロックがあるか否か
        bool isBlock;

        switch (moveDirection)
        {
        // 左に移動
        case MOVE_DIR.LEFT:
            rbody.velocity       = new Vector2(moveSpeed * -1, rbody.velocity.y);
            transform.localScale = new Vector2(1, 1);

            // 正面にブロックがないかを調べる
            isBlock = Physics2D.Linecast(
                new Vector2(transform.position.x, transform.position.y + 0.5f),
                new Vector2(transform.position.x - 0.4f, transform.position.y + 0.5f),
                blockLayer);

            if (isBlock)
            {
                moveDirection = MOVE_DIR.RIGHT;
            }

            break;

        // 右に移動
        case MOVE_DIR.RIGHT:
            rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);
            // キャラを反転させる
            transform.localScale = new Vector2(-1, 1);

            // 正面にブロックがないかを調べる
            isBlock = Physics2D.Linecast(
                new Vector2(transform.position.x, transform.position.y + 0.5f),
                new Vector2(transform.position.x + 0.4f, transform.position.y + 0.5f),
                blockLayer);

            if (isBlock)
            {
                moveDirection = MOVE_DIR.LEFT;
            }

            break;
        }
    }
Beispiel #22
0
    // Update is called once per frame
    void Update()
    {
        // transform.up is the instance of Vector3(0f, 1f, 0f).
        // transform.right is the instance of Vector3(1f, 0f, 0f).
        // Sources:
        // - https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Transform/ScriptBindings/Transform.bindings.cs
        // - https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Vector3.cs
        canJump =
            Physics2D.Linecast(
                transform.position - (transform.right * 0.3f),
                transform.position - (transform.up * 0.1f),
                blockLayer) ||
            Physics2D.Linecast(
                transform.position + (transform.right * 0.3f),
                transform.position - (transform.up * 0.1f),
                blockLayer);

        animator.SetBool("onGround", canJump);

        if (!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");

            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }

            if (Input.GetKeyDown("space"))
            {
                PushJumpButton();
            }
        }
    }
Beispiel #23
0
    // Update is called once per frame
    void Update()
    {
        if (afterStart)

        /*Updateの中に"animator.SetBool("onGround", canJump);"があるため
         * スタート時にtrueにしたが意味をなさない。なので数秒後にafterStartをtrueにしてUpdateの処理を遅らせる。
         * たぶんもっといい方法がある?はず;*/
        {
            canJump = Physics2D.Linecast(transform.position - (transform.right * 0.3f),             //position:座標、right,up:ベクトル
                                         transform.position - (transform.up * 0.1f), blockLayer) || //Linecst:(始点,終点,レイヤー)
                                                                                                    // ↑bool型。始点と終点を設定し、そこに線を張る。
                                                                                                    //コライダーがヒットしたらtrueを返す
                      Physics2D.Linecast(transform.position + (transform.right * 0.3f),
                                         transform.position - (transform.up * 0.1f), blockLayer);

            animator.SetBool("onGround", canJump);

            if (!usingButtons)
            {
                float x = Input.GetAxisRaw("Horizontal");

                if (x == 0)
                {
                    moveDirection = MOVE_DIR.STOP;
                }
                else
                {
                    if (x < 0)
                    {
                        moveDirection = MOVE_DIR.LEFT;
                    }
                    else
                    {
                        moveDirection = MOVE_DIR.RIGHT;
                    }
                }
            }
            if (Input.GetKeyDown("space"))
            {
                PushJumpButton();
            }
        }
    }
    // void Update () {
    // }

    void FixedUpdate()
    {
        bool isBlock;           // 進行方向にブロックがあるか否か

        switch (moveDirection)
        {
        case MOVE_DIR.LEFT:
            // 進行方向に速度をセット
            rbody.velocity       = new Vector2(moveSpeed * -1, rbody.velocity.y);
            transform.localScale = new Vector2(1, 1);                           // 画像を左向きにする

            // 前方への衝突判定
            isBlock = Physics2D.Linecast(
                // 前方の衝突判定(敵の背中から鼻先への線分がblockLayerに触れるか?)
                new Vector2(transform.position.x, transform.position.y + judgeHight),
                new Vector2(transform.position.x - judgeHight, transform.position.y + judgeHight),
                blockLayer);

            if (isBlock)
            {
                moveDirection = MOVE_DIR.RIGHT;
            }
            break;

        case MOVE_DIR.RIGHT:
            // 進行方向に速度をセット
            rbody.velocity       = new Vector2(moveSpeed, rbody.velocity.y);
            transform.localScale = new Vector2(-1, 1);                          // 画像を右向きにする

            // 前方への衝突判定
            isBlock = Physics2D.Linecast(
                new Vector2(transform.position.x, transform.position.y + judgeHight),
                new Vector2(transform.position.x + judgeHight, transform.position.y + judgeHight),
                blockLayer);

            if (isBlock)
            {
                moveDirection = MOVE_DIR.LEFT;
            }
            break;
        }
    }
Beispiel #25
0
    void Start()
    {
        fDeltaY     = 0;
        mMoveHeight = MOVE_DIR.HOR;
        GameObject obj = GameObject.Find("MainCamera");

        mCamera = obj.GetComponent <Camera>();
        mAni    = GetComponent <Animator>();

        if (mCamera == null)
        {
            Debug.Log("ccan found canmear");
        }
        else
        {
            mCamera.transform.position = transform.position;
        }
        //mCamera = obj.GetComponent<Camera>();
        mTa   = new Vector3();
        mTb   = new Vector3();
        mTx   = new Vector3();
        mPool = new Vector3[POOL_SIZE];

        PSIZE = FindChild();
        if (PSIZE <= 0)
        {
            Debug.Log("start track fail");
            return;
        }
        iCurrentIndex = iStartIndex % PSIZE;
        AdjustPointY();
        mDir = mPoints[(iCurrentIndex + 1) % PSIZE] - mPoints[iCurrentIndex % PSIZE];
        transform.position  = mPoints[iCurrentIndex % PSIZE];
        transform.position += Vector3.up * 3;
        for (int i = 0; i < POOL_SIZE; i++)
        {
            mPool[i] = new Vector3();
            Vector3Copy(transform.position, ref mPool[i]);
            Debug.Log("src" + transform.position.ToString() + "->" + mPool[i].ToString());
        }
        ;
    }
Beispiel #26
0
    /*===============================================================*/

    /*===============================================================*/
    /// <summary>
    /// @brief UnityEngine ライフサイクルによって毎フレーム呼ばれます
    /// </summary>
    void Update( )
    {
        // キーを取得します
        if (Input.GetKeyUp(KeyCode.D))
        {
            moveDirection = MOVE_DIR.STOP;
        }
        if (Input.GetKeyUp(KeyCode.A))
        {
            moveDirection = MOVE_DIR.STOP;
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            moveDirection = MOVE_DIR.RIGHT;
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            moveDirection = MOVE_DIR.LEFT;
        }
    }
Beispiel #27
0
    void Update()
    {
        if (!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");

            if ((x == 0))
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else if (x < 0)
            {
                moveDirection = MOVE_DIR.LEFT;
            }
            else if (x > 0)
            {
                moveDirection = MOVE_DIR.RIGHT;
            }
        }
    }
Beispiel #28
0
    // Update is called once per frame
    void Update()
    {
        canJump =
            Physics2D.Linecast(
                transform.position - (transform.right * 0.3f), transform.position - (transform.up * 0.1f),
                blockLayer
                )
            ||
            Physics2D.Linecast(
                transform.position + (transform.right * 0.3f), transform.position - (transform.up * 0.1f),
                blockLayer
                );

        // Jump状態かどうかanimatorにセット
        animator.SetBool("onGround", canJump);

        // キーボードボタン使用処理
        if (!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");
            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            }
            else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }
            if (Input.GetKeyDown("space"))
            {
                PushJumpButton();
            }
        }
    }
Beispiel #29
0
    void FixedUpdate()
    {
        bool isBlock; //進行方向にブロックがあるかどうか


        switch (moveDirection)
        {
        case MOVE_DIR.LEFT:                           //左に移動
            rbody.velocity       = new Vector2(moveSpeed * -1, rbody.velocity.y);
            transform.localScale = new Vector2(1, 1); // localPosition = 親の Transform オブジェクトから見た相対的なスケール
            //↑これで左右の反転を行っている

            //Linecast(bool型)を使ってキャラクター前方に衝突判定を与える
            isBlock = Physics2D.Linecast        //(始点、終点、レイヤー) レイヤーは衝突の対象となるもの
                          (new Vector2(transform.position.x, transform.position.y + 0.5f),
                          new Vector2(transform.position.x - 0.3f, transform.position.y + 0.5f), blockLayer);

            if (isBlock)       //ぶつかったら移動方向反転
            {
                moveDirection = MOVE_DIR.RIGHT;
            }
            break;

        case MOVE_DIR.RIGHT:                           //右に移動
            rbody.velocity       = new Vector2(moveSpeed * 1, rbody.velocity.y);
            transform.localScale = new Vector2(-1, 1); //反転

            isBlock = Physics2D.Linecast
                          (new Vector2(transform.position.x, transform.position.y + 0.5f),
                          (new Vector2(transform.position.x + 0.3f, transform.position.y + 0.5f)), blockLayer);

            if (isBlock)
            {
                              //ぶつかったら移動方向反転
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
            }
            break;
        }
    }
    private void Update()
    {
        //canJumpの更新
        canJump = Physics2D.Linecast(transform.position - (transform.right * 0.3f),
                                     transform.position - (transform.up * 0.05f), blockLayer) ||
                  Physics2D.Linecast(transform.position + (transform.right * 0.3f),
                                     transform.position - (transform.up * 0.05f), blockLayer);

        //キー入力検知
        moveDirection = MOVE_DIR.STOP;
        if ((Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) && canJump)
        {
            goJump = true;
        }
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            moveDirection = MOVE_DIR.LEFT;
        }
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            moveDirection = MOVE_DIR.RIGHT;
        }
    }