实验2:仿真系统功能实现
(1)熟悉在Unity中设置仿真场景;
(2)熟悉在Unity中C#语言的使用;
(3)熟悉仿真功能的实现。
新建一个仿真场景,完成下列功能:
(1)使用Unity的基本建模功能设置一些三维场景(自行发挥想象,进行建模设计)
(2)实现漫游功能,可以在场景中键盘控制前后左右移动,鼠标控制旋转,完成基本的场景漫游功能。(自行设计)
(3)使用射线,实现获取鼠标的点击功能。(自行设计)
(4)制作内部动画,配合鼠标点击,实现播放动画。(自行设计)
(5)设置触发器,当漫游相机进到入触发器中时,执行动画的播放。(自行设计)
(6)添加背景音乐和鼠标点击的音效(自行设计)
(7)添加UI按钮设计,要求可以返回主控界面。(自行设计)
(8)打包,生成可执行文件,要求可执行文件脱离Unity环境后,能够自行运行。
创建一个新的场景用来构建三维场景

接着添加地面

接着,可以通过对象中自带的组件去改变地形

通过组件中自带的功能 可以调整地形,构建出自己需要的地势
接着,通过增加树木花草等,构建出基本的三维场景
这里我使用的是另一种方法,可以去素材商城中添加一下免费的素材,可以方便搭建场景。

将该代码附加加需要控制的物体上,即可实现控制物体移动。
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
-
- public class Component1 : MonoBehaviour
- {
- GameObject obj;
- public float floSpeed = 10;
- public float floRotate = 100;
-
- void Start()
- {
- //第三人称视角跟随
- Camera.main.transform.SetParent(this.transform);
- Camera.main.transform.localPosition = new Vector3(0, 3, -4);
- Camera.main.transform.localEulerAngles = new Vector3(6, 0, 0);
-
-
- }
-
- }
-
- void Update()
- {
-
- move();
- look();
- }
-
- private void look()
- {
- float y = Input.GetAxis("Mouse X") * floRotate * Time.deltaTime;
- transform.Rotate(transform.up, y);
-
- }
-
- private void move()
- {
-
- float x =Input.GetAxis("Horizontal") *floSpeed* Time.deltaTime;
- float z =Input.GetAxis("Vertical") *floSpeed* Time.deltaTime;
- transform.Translate(x, 0, z);
-
-
- }
- }
当点击地面时候,返回地面的坐标。

可以通过右键--->Create创建一个Animation
为了方便后面的动画设计,我们这里导入一个素材

通过设置动画执行顺序 和条件,完成捡东西动画的制作

右键--->Audio 添加一个音乐组件
导入音乐素材,拖入Audio组件中
- 通过按B去设置背景音乐的播放
-
- private void playMusic()
- {
- if (Input.GetKey(KeyCode.B))
- {
- if(ads.isPlaying)
- {
- ads.Pause();
- }
- else
- {
- ads.Play();
- }
- }
- }
- 通过ZX去调节音量大小
- private void changeVolume()
- {
- if (Input.GetKey(KeyCode.Z))
- {
- ads.volume--;
- }else if (Input.GetKey(KeyCode.X))
- {
- ads.volume++;
- }
-
- }
第六步:添加UI按钮设计,要求可以返回主控界面。

添加UI button按钮,设计一个返回按钮
编辑代码,当点击按钮时候 跳转到登录的场景
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro; //新版
- using UnityEngine.UI; //旧版GUI命名空间
- using System;
- using UnityEngine.SceneManagement;
-
- public class Component11 : MonoBehaviour
- {
- public Button button;
- // Start is called before the first frame update
- void Start()
- {
- button.onClick.AddListener(ClickButton);
- }
-
- // Update is called once per frame
- void ClickButton()
- {
- SceneManager.LoadScene(0);
- }
- }