>本教程增强了建立在团结的基本基本克隆,添加了经典的两个美学,改进的游戏机制和复古风格的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中文网其他相关文章!