**
用Unity 3D做簡單的吃金幣游戲步驟如下
**
1、做出大致場景

2、金幣旋轉
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class xuanzhuan : MonoBehaviour
{
public float xz;
// Start is called before the first frame update
void Start()
{
xz = 120.0f; //旋轉速度即角速度
}
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, 1, 0) * xz * Time.deltaTime); //繞Y軸旋轉
}
}
3、小球運動利用重力系統
1、先添加重力系統在屬性欄的 Add Componet 添加

2、代碼實作小球運動和碰到金幣消失
下面展示一些 行內代碼片,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class frist : MonoBehaviour
{
public Rigidbody rg;
public float sudu;
// Start is called before the first frame update
void Start()
{
sudu = 20.0f; //小球速度
rg = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// 運動和鍵盤系結前后左右跳(W,S,A,D,Speca)
if (Input.GetKey(KeyCode.W))
{
rg.AddForce(Vector3.forward * Time.deltaTime * sudu);
//transform.Translate(Vector3.forward * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.A))
{
rg.AddForce(Vector3.left * Time.deltaTime * sudu);
//transform.Translate(Vector3.left * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.S))
{
rg.AddForce(Vector3.back * Time.deltaTime * sudu);
//transform.Translate(Vector3.back * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.D))
{
rg.AddForce(Vector3.right * Time.deltaTime * sudu);
//transform.Translate(Vector3.right * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.Space))
{
transform.Translate(Vector3.up * Time.deltaTime * sudu);
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject); // 碰到消失
}
3‘注意金幣屬性欄要勾選 Is Trigger 不然不會消失

4、相機跟隨運動物體
下面展示一些 行內代碼片,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gen : MonoBehaviour
{
public Vector3 dis;
// dis儲存距離(三維坐標)
public GameObject ball;
// ball儲存要跟隨的游戲物件
// Start is called before the first frame update
void Start()
{
dis = ball.transform.position - transform.position;
// 獲取自身與小球的距離并賦值給dis
}
// Update is called once per frame
void Update()
{
transform.position = ball.transform.position - dis;
// 自身的坐標等于小球坐標減去開始的距離
}
}
注意屬性欄中的要把運動物體拖入ball 我自己的運動物體名稱是Sphere

5、使用UI界面顯示得分開始開始暫停最終效果如圖**

1、新建UI界面
右鍵雙擊Create空白處找到UI在找到text

2、分數和最后吃完出現文字代碼
是在小球物體中寫的
下面展示一些 行內代碼片,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //引入命名空間
public class frist : MonoBehaviour
{
private int score;
public Text fengshu;
public Text ying;
public Rigidbody rg;
public float sudu;
// Start is called before the first frame update
void Start()
{
ying.gameObject.SetActive(false); //開始不顯示這個文字
score = 0; //得分為0
sudu = 20.0f;
rg = GetComponent();
}
// Update is called once per frame
void Update()
{
}
if (Input.GetKey(KeyCode.W))
{
rg.AddForce(Vector3.forward * Time.deltaTime * sudu);
//transform.Translate(Vector3.forward * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.A))
{
rg.AddForce(Vector3.left * Time.deltaTime * sudu);
//transform.Translate(Vector3.left * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.S))
{
rg.AddForce(Vector3.back * Time.deltaTime * sudu);
//transform.Translate(Vector3.back * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.D))
{
rg.AddForce(Vector3.right * Time.deltaTime * sudu);
//transform.Translate(Vector3.right * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.Space))
{
transform.Translate(Vector3.up * Time.deltaTime * sudu);
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
//開始計算得分
fengshu.text = "分數:" + ++score;
// 用判斷語氣 判斷是否全部吃完
if (score ==5)
{
ying.gameObject.SetActive(true); //顯示文字
}
}
在運動無題的屬性欄中的代碼方法中拖入相應的名稱我這里分數是Text, 贏是Text(1) 如下圖


3、添加Button 一個是開始一個是退出

1、退出代碼
下面展示一些 行內代碼片,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; //引入編輯變數
public class Quit : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void BtnQuit()
{
//退出播放即退出游戲
EditorApplication.isPlaying = false;
// 如果要打包成exe檔案則可以退出應用程式
// Application.Quit();
}
}
2、開始代碼
下面展示一些 行內代碼片,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;//引入編輯場景變數
public class kais : MonoBehaviour
{
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GetScene()//
{
EditorSceneManager.LoadScene("s1");
}
}
注意要在相應空間的屬性欄中的的on click() 先添加右下角的+號添加然后再把拖入屬性欄的代碼再拖入圖片中

在
這里找到你寫的方法我這里的方法是BtnQuit.
開始控制元件也一樣操作
6、實作通過鍵盤來來實作暫停和繼續運動
代碼如下也是寫在運動物體代碼中
下面展示一些 行內代碼片,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class frist : MonoBehaviour
{
public Button tuichu;
public Button kais;
public bool isPlay;
private int score;
public Text fengshu;
public Text ying;
public Rigidbody rg;
public float sudu;
// Start is called before the first frame update
void Start()
{
Time.timeScale = 1;//判斷是否運動的標志時間1為運動
isPlay = true; //用布爾來判斷按一下暫停再按一下運動
tuichu.gameObject.SetActive(false);//運動是時控制元件隱藏
kais.gameObject.SetActive(false);//運動時控制元件隱藏
ying.gameObject.SetActive(false);
score = 0;
sudu = 20.0f;
rg = GetComponent();
}
// Update is called once per frame
void Update()
{
// 按esc來實作暫停和繼續
if(Input.GetKeyDown(KeyCode.Escape))
{
if(isPlay)
{
Time.timeScale = 0; //暫停運動·
tuichu.gameObject.SetActive(true); //控制元件出現
kais.gameObject.SetActive(true);//控制元件出現
isPlay = !isPlay;
}
else
{
Time.timeScale = 1; //繼續運動
isPlay = !isPlay;
tuichu.gameObject.SetActive(false); //控制元件隱藏
kais.gameObject.SetActive(false);//控制元件隱藏
}
}
if (Input.GetKey(KeyCode.W))
{
rg.AddForce(Vector3.forward * Time.deltaTime * sudu);
//transform.Translate(Vector3.forward * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.A))
{
rg.AddForce(Vector3.left * Time.deltaTime * sudu);
//transform.Translate(Vector3.left * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.S))
{
rg.AddForce(Vector3.back * Time.deltaTime * sudu);
//transform.Translate(Vector3.back * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.D))
{
rg.AddForce(Vector3.right * Time.deltaTime * sudu);
//transform.Translate(Vector3.right * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.Space))
{
transform.Translate(Vector3.up * Time.deltaTime * sudu);
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
fengshu.text = "分數:" + ++score;
if (score ==5)
{
ying.gameObject.SetActive(true);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/157532.html
標籤:其他
