本科课程【虚拟现实引擎Unity3D】实验2 - 复杂地形穿越

x33g5p2x  于2022-05-13 转载在 其他  
字(2.2k)|赞(0)|评价(0)|浏览(207)

大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。
如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!!
Good better best, never let it rest, until good is better, and better best.

近期会把自己本科阶段的一些课程设计、实验报告等分享出来,供大家参考,希望对大家有帮助。

博客更新至专栏【课程设计实验报告】:https://blog.csdn.net/weixin_43598687/category_11640051.html

一、 实验目的

  1. 掌握地形的创建,实现游戏人物在复杂地形的穿越

二、 实验内容

1. 实验任务

自己搭建一个有障碍或陷阱等元素的地形,有玩家在地形中穿越的出发点及终点,在穿越过程中碰到障碍或掉入陷阱则视为穿越失败一次,玩家自动回到出发点重新开始穿越。失败次数超过某个设定值或到达终点,都结束一次游戏过程,相应显示成功或失败的相关统计信息(如用时,失败次数等)并可重新开始新的一次游戏。玩家可以是第一人称或第三人称的角色控制器。加入必要游戏对象。模型及编写有关脚本等,能通过键盘和鼠标相结合进行穿越。可根据需要增加其他内容。

2. 程序设计

1) 数据输入
初始化输入

2) 数据存储(输入数据在内存中的存储)
player存储游戏对象,forest1,forest2,forest3分别存储3个森林地形的对象,并用forests[]数组存储起来,达到三个地形重复交替出现,forest存储森林出现的次数。

3) 数据处理

//控制森林出现和消失的条件
if (player.position.z > transform.position.z+100)
        {
            Camera.main.SendMessage("GenerateForest");
            GameObject.Destroy(this.gameObject);
        }
//控制森林的重复出现
    public void GenerateForest()
    {
        forestCount++;
        int type=Random.Range(0,3);
        GameObject newForest=GameObject.Instantiate(forests[type],new Vector3(0,0,forestCount*3000),Quaternion.identity) as GameObject;
        forest1 = forest2;
        forest2 = newForest.GetComponent<Forest>();
    }

4) 数据输出

三、 实验环境

  1. 操作系统:WINDOWS 7及以上
  2. 开发工具:Unity
  3. 实验设备:PC

源代码

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

public class EnvGenerater : MonoBehaviour {
    public Forest forest1;
    public Forest forest2;
    public int forestCount = 2;
    public GameObject[] forests;
    public void GenerateForest()
    {
        forestCount++;
        int type=Random.Range(0,3);
        GameObject newForest=GameObject.Instantiate(forests[type],new Vector3(0,0,forestCount*3000),Quaternion.identity) as GameObject;
        forest1 = forest2;
        forest2 = newForest.GetComponent<Forest>();
    }
}

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

public class Tags : MonoBehaviour {
    public const string player = "Player";
}

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

public class Forest : MonoBehaviour {
    private Transform player;
    void Awake() {
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;
    }
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (player.position.z > transform.position.z+100)
        {
            Camera.main.SendMessage("GenerateForest");
            GameObject.Destroy(this.gameObject);
        }
	}
}

博客更新至专栏【课程设计实验报告】:https://blog.csdn.net/weixin_43598687/category_11640051.html

相关文章