Unity游戲:一跳到底游戲制作
教程目錄:
- Unity游戲:一跳到底游戲制作
- 一、Unity游戲:一跳到底——創建新專案、匯入資源、設定場景
- 1.創建新專案
- 2.匯入資源
- 3.設定場景
- 二、Unity游戲:一跳到底——制作游戲場景、制作地板預制體、創建小球
- 1、制作游戲場景
- 2、制作地板預制體
- 3、創建小球
- 三、Unity游戲:一跳到底——小球的彈跳、相機的跟隨、圓柱的旋轉
- 1、小球的彈跳
- 2、相機的跟隨(該代碼加入相機屬性欄中)
- 3、圓柱的旋轉(該代碼加入圓柱屬性欄中)
- 四、Unity游戲:一跳到底——得分檢測、失敗檢測
- 1、得分檢測
- 2、失敗檢測
- 五、Unity游戲:一跳到底——開始場景制作、圓柱隨機旋轉和移動、按鈕功能的實作
- 1、開始場景制作
- 2、圓柱的隨機旋轉和移動
- 3、按鈕功能的實作
- 六、Unity游戲:一跳到底——添加背景圖片、音樂效果、小球顏色拖尾效果
- 1、添加背景圖片
- 2、音樂效果
- 3、小球顏色拖尾效果
一、Unity游戲:一跳到底——創建新專案、匯入資源、設定場景
1.創建新專案
File -> New project -> 輸入Project name -> Preate project
2.匯入資源
鏈接:https://pan.baidu.com/s/17EIGiDtIUNpcPM2zB-XRUw
提取碼:x8kk
復制這段內容后打開百度網盤手機App,操作更方便哦

3.設定場景
保存場景
File -> Save Scene / 直接Ctrl+S -> 輸入檔案名“Game” -> 保存
保存完之后在Assets檔案夾可以看到剛剛保存的場景,

設定build target
File -> Build Settings
默認的build平臺是PC, Mac & Linux Standalone,也可以選擇其他平臺,例如iOS、Android等等,

可以在Build Settings視窗中點擊Player Setting設定更多細節,
Player Setting也可以在Edit -> Project Settings -> Player中打開,

將寬高比設定為600*900:

二、Unity游戲:一跳到底——制作游戲場景、制作地板預制體、創建小球
1、制作游戲場景
創建一個圓柱

y 的大小設定為16

2、制作地板預制體
1、匯入的資源包中找到合適的圓柱形面板進行組合(不想做這一步可以用博主做好的預制體)

2、相應的每塊圓柱形面板加入collider組件并勾選Trigger

3、每種看自己喜好給顏色材質并都做成預制體(建議顏色種類不要太多)

4、然后做成如下樣子(每個面板留一個口子方便小球小落)
3D場景圖

2D場景圖
每一層搞到一個空物體中去

3、創建小球
創建小球并給小球賦予材質和重力組件(小球名字為Plyer)



三、Unity游戲:一跳到底——小球的彈跳、相機的跟隨、圓柱的旋轉
1、小球的彈跳
1、新建腳本名為Player
2、為黑色面板添加標簽Plane

3、小球接觸面板涂鴉預制體

4、代碼撰寫
private Rigidbody rg;
//小球的運動速度
public float speed;
void Start()
{
rg = GetComponent<Rigidbody>();
jumpMusic = GetComponent<AudioSource>();
speed = 6.8f;(速度自己調)
}
private void OnTriggerEnter(Collider col)
{
//小球碰到面板
if (col.tag == "Plane")
{
//小球向上運動
rg.velocity = Vector3.up * speed;
// 涂鴉生產(產生的位置根據自己小球的大小設定)
GameObject color = Instantiate(colorPrefab,
new Vector3(transform.position.x, col.transform.position.y+0.5f,
transform.position.z) , colorPrefab.transform.rotation);
Destroy(color,0.2f);
}
}
2、相機的跟隨(該代碼加入相機屬性欄中)
1、代碼撰寫(腳本名為Cam)
public Transform ball;
private Vector3 dis;
// Start is called before the first frame update
void Start()
{
//獲得小球與相機的偏移量
dis = ball.position - transform.position;
}
// Update is called once per frame
void Update()
{
if (Mathf.Abs(transform.position.y-ball.position.y)>1.5f)
{
//得到相機目標的位置
Vector3 target = ball.position - dis;
// 插值法使相機移動到目標位置
transform.position = Vector3.Lerp(transform.position, target, Time.deltaTime*6);
}
}
3、圓柱的旋轉(該代碼加入圓柱屬性欄中)
1、代碼撰寫(腳本名為XZ)
private float x;
public float speed;
public GameObject colorPrefab;
// Start is called before the first frame update
void Start()
{
// 旋轉速度
speed = 60;
float y = Random.Range(-90, 90);
//初始位置隨機繞Y軸旋轉
transform.Rotate(new Vector3(0, y, 0));
}
// Update is called once per frame
void Update()
{
//通過滑鼠左鍵控制旋轉
if (Input.GetMouseButton(0))
{
// 通過虛擬軸獲取滑鼠水平方向的移動增量
x = Input.GetAxis("Mouse X");
//transform.localEulerAngles = new Vector3(0, -x*speed, 0);
// 通過Rotate結合滑鼠左鍵的移動增量旋轉
transform.Rotate(new Vector3(0, x * -1, 0) * speed * Time.deltaTime);
}
}
四、Unity游戲:一跳到底——得分檢測、失敗檢測
1、得分檢測
1、創建空物體命名為并給它加Box Collider組件還要添加標簽Score

2、檢測范圍調到自己適合大的大小有四個

3、得分UI顯示文本命名為ScoreText并且文字大小和位子自己調



4、代碼撰寫(在Plyer腳本撰寫)
using UnityEngine.UI;// 引入命名空間
// 得分UI
public Text scoreText;
public int score;
private void OnTriggerEnter(Collider col)
{
//小球得分檢測
if (col.tag == "Score")
{
SetScore();
}
}
//得分方法
public void SetScore()
{
score++;
if (scoreText != null)
scoreText.text = score.ToString();
}
2、失敗檢測
1、給黃色面板預添加標簽Error


2、失敗按鈕UI制作退出和重開按鈕

3、給按鈕添加背景圖示



4、代碼撰寫
using UnityEngine.UI;
public Button start;
public Button exit;
void Start()
{
// 游戲開始按鈕不顯示
start.gameObject.SetActive(false);
exit.gameObject.SetActive(false);
}
private void OnTriggerEnter(Collider col)
{
//小球游戲失敗檢測
if (col.tag == "Error")
{
// 按鈕顯示
start.gameObject.SetActive(true);
exit.gameObject.SetActive(true);
transform.position = new Vector3(transform.position.x,
transform.position.y + 0.2f, transform.position.z);
//游戲暫停
Time.timeScale = 0;
}
5、將相應的文本或者按鈕和預制體拖入小球代碼組件里去

五、Unity游戲:一跳到底——開始場景制作、圓柱隨機旋轉和移動、按鈕功能的實作
1、開始場景制作
1、新建場景命名為Main

2、場景大小和游戲場景一樣給場景添加背景圖片和開始按鈕




3、將場景加載拖入箭頭所指中去

2、圓柱的隨機旋轉和移動
1、給圓柱所在空物體添加Box Collider組件并給空物體添加標簽Pipe(圖中最大綠色長方體)

2、兩個圓柱都有
3、代碼撰寫圓柱隨機旋轉和無限下移
private void OnTriggerEnter(Collider col)
{
// 管子移動給管子加了box Collider組件
if (col.tag == "Pipe")
{
//隨機繞著Y軸旋轉
float y = Random.Range(-90, 90);
transform.Rotate(new Vector3(0, y, 0));
//在自身位置上y軸的位置減去32得到無限向下產生(自己根據場景中的圓柱坐標進行設定)
col.transform.position = new Vector3(col.transform.position.x, col.transform.position.y - 32,
col.transform.position.z);
}
}
3、按鈕功能的實作
1 代碼撰寫新建兩個腳本名字為Exit和GameStart
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Exit : MonoBehaviour
{
public void ExitGame()
{
//退出
EditorApplication.isPlaying = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;
public class GameStart : MonoBehaviour
{
public void Game()
{
//重新開始加載場景
EditorSceneManager.LoadScene(0);
// 游戲進行
Time.timeScale = 1;
}
}
2、按鈕功能系結(對應好)

找到代碼中的方法

六、Unity游戲:一跳到底——添加背景圖片、音樂效果、小球顏色拖尾效果
1、添加背景圖片

2、音樂效果
1、背景音樂給相機添加相應組件(Play On Awake 和Loop要勾選)

2、彈跳音樂給小球添相應空間并拖拽音樂(Play On Awake 和Loop要不用勾選)

3、代碼實作
//彈跳音樂
private AudioSource jumpMusic;
private void OnTriggerEnter(Collider col)
{
//小球碰到面板
if (col.tag == "Plane")
{
//小球向上運動
rg.velocity = Vector3.up * speed;
// 涂鴉生產
GameObject color = Instantiate(colorPrefab,
new Vector3(transform.position.x, col.transform.position.y+0.5f, transform.position.z)
, colorPrefab.transform.rotation);
Destroy(color,0.2f);
jumpMusic.Play();
}
if (col.tag == "Error")
{
//背景音樂暫停
GameObject.Find("Main Camera").GetComponent<AudioSource>().Stop();
// 按鈕顯示
start.gameObject.SetActive(true);
exit.gameObject.SetActive(true);
}
}
3、小球顏色拖尾效果
可以去博主(柚子味的小檸檬)看制作方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/273245.html
標籤:python
