Roll a Ball

一、實驗要求

  1. 構(gòu)建一個小球滾動的游戲場景;
  2. 創(chuàng)建一個小球,按鍵盤上的上下左右鍵,小球會朝相應(yīng)的方向移動,小球移動的時候相機也要相應(yīng)移動;
  3. 在場景中創(chuàng)建多個立方體,每個立方體都在旋轉(zhuǎn);小球與立方體發(fā)生碰撞的時候,立方體消失,計分板上得分加“1”;
  4. 當(dāng)?shù)梅诌_到“5”分時,在屏幕上顯示“XXX同學(xué),你贏了!”。

加分項目:

  1. 添加小球和立方體發(fā)生碰撞的特效,添加立方體隨機生成,添加小球撞擊阻礙物的物理效果。
  2. 你能想到的可以實現(xiàn)的其他效果。
游戲場景

二、小球設(shè)置 Player & 碰撞特效

2.1 上下左右移動

Player.cs 設(shè)置前后左右移動,以及 force 調(diào)節(jié)力大小。

// Update is called once per frame 持續(xù)調(diào)用的命令
void Update () {
    
    // 通過鍵盤控制移動

    float h = Input.GetAxis("Horizontal"); // 得到水平軸的值 [-1,1] D正向, A負(fù)向

    float v = Input.GetAxis("Vertical"); // W S

    rd.AddForce(new Vector3(h, 0, v) * force); // 施加力 向量表示方向 控制運動方式 *force 快速更改方向

    // eatText.GetComponent<Text>().text = "bingo!!"; // 設(shè)置GameObject的文本
}

2.2 小球移動,攝像機跟隨

FollowTarget.cs

FollowTarget.cs 讓 Main Camera 跟隨小球 Player 移動

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// camera script
public class FollowTarget : MonoBehaviour {

    public Transform playerTransform; // player的Transform 把Player從面板上拖過去

    private Vector3 offset; // 相機和小球最開始的偏移

    // Use this for initialization
    void Start () {
        // 計算初始的時候位置的偏移
        offset = transform.position - playerTransform.position; // main camera - player 向量差
    }
    
    // Update is called once per frame
    void Update () {
        // player current pos + offset = camera current pos
        transform.position = playerTransform.position + offset;
    }
}

2.3 用prefab創(chuàng)建多個旋轉(zhuǎn)的立方體

PickUps

PickUp.cs 讓每個立方體旋轉(zhuǎn)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// pickup script
public class PickUp : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () { // 調(diào)用 60次/s   1°/s
        transform.Rotate(new Vector3(0, 1, 0)); // 繞著y軸旋轉(zhuǎn)
    }
}

2.4 觸發(fā)檢測 添加 碰撞特效

2.4.1 坦克大戰(zhàn)爆炸特效
TankExplosion effect

Player.cs 在 OnTriggerEnter 中添加特效

爆炸特效
2.4.2 eat 文字彈出動畫
EatText
eat 放縮動畫

Player.cs 控制 eat 動畫在進入觸發(fā)器是出現(xiàn),離開觸發(fā)器時消失。

eat 彈出特效
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容