>本教程增強了建立在團結的基本基本克隆,添加了經典的兩個美學,改進的遊戲機制和復古風格的UI。 讓我們升級我們的乒乓球!
>第2部分:Retro Revamp&Enhanced Gameplay
>>本教程建立在先前的乒乓球上。 從Github下載項目或在繼續之前完成第1部分。 還可以使用工作的演示和最終改進的遊戲。
>密鑰改進:
BallController
腳本的OnCollisionEnter2D
方法以使用launchAngle
>函數。請記住適當地標記玩家和敵人槳。 EnemyController
,調整速度和時機以獲得最佳平滑度。 添加邊界以防止AI槳在屏幕上脫離。 InvokeRepeating("Move", .02F, .02F)
腳本應在銷毀球後重置敵方槳的位置。
BoundsController
UIManager
>AutoPlayer
> AutoEnemy
評分系統:PointCounter
GameOver
代碼片段:
(ballcontroller.cs- oncollisionEnter2d&啟動angle)
void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Enemy") { float y = launchAngle(transform.position, col.transform.position, col.collider.bounds.size.y); Vector2 d = new Vector2(1, y).normalized; rig2D.velocity = d * speed * 1.5F; } if (col.gameObject.tag == "Player") { float y = launchAngle(transform.position, col.transform.position, col.collider.bounds.size.y); Vector2 d = new Vector2(-1, y).normalized; rig2D.velocity = d * speed * 1.5F; } } float launchAngle(Vector2 ball, Vector2 paddle, float paddleHeight) { return (ball.y - paddle.y) / paddleHeight; }
(eneycontroller.cs-移動)
void Move() { if (ball == null) { ball = GameObject.FindGameObjectWithTag("Ball").transform; } ballRig2D = ball.GetComponent<Rigidbody2D>(); if (ballRig2D.velocity.x > 0) { if (ball.position.y > this.transform.position.y + .5F) { transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (ball.position.y < this.transform.position.y - .5F) { transform.Translate(Vector3.down * speed * Time.deltaTime); } } // ... bounds checking ... }
(uimanager.cs- partial)
// ... other methods ... public void showFinished() { foreach (GameObject g in finishObjects) { g.SetActive(true); } } // ... other methods ...
void Update () { text.text = rightBound.GetComponent<BoundController>().enemyScore + "\t\t" + leftBound.GetComponent<BoundController>().playerScore; }
(gameover.cs)
>void Update () { if(uiManager.playerWon){ text.text = "GAME OVER!\nPLAYER WON!"; } else if(uiManager.enemyWon){ text.text = "GAME OVER!\nENEMY WON!"; } }
記住在AI腳本中調整速度和計時等值以微調遊戲難度。 這種增強的乒乓球可提供更完整,更具吸引力的複古遊戲體驗。 GitHub鏈接(輸入中未提供)將包含完整的項目文件。
以上是建立UI和遊戲玩法的乒乓球克隆的詳細內容。更多資訊請關注PHP中文網其他相關文章!