void Start() { // リジボの取得 rb = GetComponent <Rigidbody2D>(); // 進行方向の取得 beforeDis = dis; // 座標の取得 beforePos = transform.position; }
// 進行方向の決定 bool DistanceDecision(GameObject obj) { Pipe.Pipe_Status status = obj.GetComponent <Pipe>().status; pipeCenterPos = obj.transform.position; centerMoveFlg = false; // 進行方向通りに進めればtrue、そうでなければfalse bool moveOnFlg = true; // 進行方向通りに進めない場合 switch (dis) { case MOVE_DISTANCE.TOP: if (true != status.down) { beforeDis = dis; dis = MOVE_DISTANCE.DOWN; moveOnFlg = false; } break; case MOVE_DISTANCE.DOWN: if (true != status.top) { beforeDis = dis; dis = MOVE_DISTANCE.TOP; moveOnFlg = false; } break; case MOVE_DISTANCE.RIGHT: if (true != status.right) { beforeDis = dis; dis = MOVE_DISTANCE.LEFT; moveOnFlg = false; } break; case MOVE_DISTANCE.LEFT: if (true != status.left) { beforeDis = dis; dis = MOVE_DISTANCE.RIGHT; moveOnFlg = false; } break; default: break; } return(moveOnFlg); }
//当たり判定 void OnCollisionStay2D(Collision2D collision) { // パイプの中心まできたか判断 if (pipeCenterPos.x == transform.position.x && pipeCenterPos.y == transform.position.y && false == centerMoveFlg) { centerMoveFlg = true; } else { transform.position = Vector3.MoveTowards(transform.position, pipeCenterPos, speed * Time.deltaTime); } if (centerMoveFlg) { // 進行方向の再算出 if (MOVE_DISTANCE.NONE == dis && "Pipe" == collision.gameObject.tag) { Pipe.Pipe_Status status = collision.gameObject.GetComponent <Pipe>().status; if (MOVE_DISTANCE.RIGHT == beforeDis && true == status.right) { dis = MOVE_DISTANCE.RIGHT; beforeDis = dis; } else if (MOVE_DISTANCE.LEFT == beforeDis && true == status.left) { dis = MOVE_DISTANCE.LEFT; beforeDis = dis; } else if (MOVE_DISTANCE.TOP == beforeDis || MOVE_DISTANCE.DOWN == beforeDis) { if (true == status.right && false == status.left) { dis = MOVE_DISTANCE.RIGHT; } else if (false == status.right && true == status.left) { dis = MOVE_DISTANCE.LEFT; } else if (true == status.right && true == status.left) { // 2つに分かれる処理 } } } } }
// 動作処理 void Move() { // 下方向 if (beforePos.y > transform.position.y + 0.5f || beforePos.y < transform.position.y - 0.5f) { dis = MOVE_DISTANCE.DOWN; descendingFlg = true; } else if (MOVE_DISTANCE.DOWN == dis && true == descendingFlg && beforePos.y == transform.position.y) { dis = MOVE_DISTANCE.NONE; descendingFlg = false; } // どの方向に移動するか switch (dis) { case MOVE_DISTANCE.TOP: rb.velocity = new Vector2(rb.velocity.x, speed); break; case MOVE_DISTANCE.DOWN: //rb.velocity = new Vector2(rb.velocity.x, -speed); break; case MOVE_DISTANCE.RIGHT: rb.velocity = new Vector2(speed, rb.velocity.y); break; case MOVE_DISTANCE.LEFT: rb.velocity = new Vector2(-speed, rb.velocity.y); break; default: break; } if (0 == descendingCnt % 30) { beforePos.y = transform.position.y; } descendingCnt++; }