2020-10-04

    科技2022-07-14  119

    **

    用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); } }
    Processed: 0.012, SQL: 8