提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
unity第四次实验作业
1.基本操作演练
*下载Fantasy Skybox FREE(是一个天空背景图的集合,拖到游戏编辑界面就可以运行)
下载地址:链接: link
但是可能打开的巨慢
*自己做360全景图材料(没有美术功底的还是选择上面的free包)
编程实验中本来已经import了free包但是整个assets文件变的很大就删了,在网上找了一个图做背景,效果比原来差远了
2.编程实践:(二选一)
1,魔鬼2,动作分离,增加actioncontrol类,并在firstcontrol和role中调用(后附加代码)
2,《大主宰》关卡设计
提示:以下是本篇文章正文内容,下面案例可供参考
一、编程实验,魔鬼2
基于上次的魔鬼1代码进行了微调:
添加了actioncontrol的c#文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActionController
{
public void MoveToBoat(Role role, int i)
{
role.setPos(2);
if (i == 1){
role.setBoatPos(0);
role.obj.transform.position = new Vector3(0, -3, 4);}
else if(i == 2){
role.setBoatPos(1);
role.obj.transform.position = new Vector3(0, -3, 6);}
else if(i == 3){
role.setBoatPos(1);
role.obj.transform.position = new Vector3(0, -3, -4);}
else{
role.setBoatPos(0);
role.obj.transform.position = new Vector3(0, -3, -6);}
}
public void MoveGroundFrom(Role role)
{
role.obj.transform.position = role.getGroundPos(0);
role.setPos(0);
}
public void MoveGroundTo(Role role)
{
role.obj.transform.position = role.getGroundPos(1);
role.setPos(1);
}
public void MoveBoat(Boat boat, Role []Priests, Role []Devils)
{
Vector3 currentPos = boat.boat_obj.transform.position;
Vector3[] boat_park = { new Vector3(0, -4, -5), new Vector3(0, -4, 5) };
Vector3 park_now = boat_park[boat.movestate - 1];
int boat_pos = boat.movestate - 1;
if (currentPos == park_now){
boat.movestate = 0;
}
boat.boat_obj.transform.position = Vector3.MoveTowards(currentPos, park_now, 7f * Time.deltaTime);
Vector3[,] seats = {
{ new Vector3(0, -3, -6), new Vector3(0, -3, -5) },
{ new Vector3(0, -3, 5), new Vector3(0, -3, 6) }
};
for (int i = 0;i < 2;i++){
int seat_pos = i;
Role role_t = MapNumToRole(boat.BoatState[i], Priests, Devils);
if (role_t != null)
{
Vector3 cur = role_t.obj.transform.position;
role_t.obj.transform.position = Vector3.MoveTowards(cur, seats[boat_pos,seat_pos], 7f * Time.deltaTime);
}
}
}
Role MapNumToRole(int num, Role []Priests, Role []Devils)
{
Role role_temp = null;
if(num < 4 && num > 0){
role_temp = Priests[num - 1];
}
else if(num >= 4 && num <= 6){
role_temp = Devils[num - 4];
}
if(role_temp == null){
}
return role_temp;
}
}
修改了firstcontrol中的调用(针对actioncontrol的调用)
新增actioncontrol对象
private ActionController actionController;//调用action
start函数调用
void Start() {
Director director = Director.GetInstance();
director.scene = this;
this.LoadResources();
actionController = new ActionController();
}
update()
void Update () {
if(boat.movestate > 0){
actionController.MoveBoat(boat, Priests, Devils);
}
//caipan
if ((fromNum_priest != 0 && fromNum_devil > fromNum_priest) || (toNum_priest != 0 && toNum_devil > toNum_priest)){
state = 0;
gui.setLose();
}
if (toNum_devil == 3 && toNum_priest == 3 && boat.num == 0){
state = 1;
gui.setWin();
}
}
修改了role的对两岸的祭师与恶魔数的判断,对船的信息判断
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Role {
public GameObject obj;
public int position;//对象 ,0代表左侧,1代表右侧,2代表boat
public int boatPos;//船
private Move move;
private Vector3 PosFrom;//左河岸
private Vector3 PosTo;//右河岸
public Role(string name)
{
obj = Object.Instantiate(Resources.Load(name, typeof(GameObject)), Vector3.zero, Quaternion.identity, null) as GameObject;
position = 0;
move = obj.AddComponent(typeof(Move)) as Move;
}
public void setName(string name)
{
obj.name = name;
}
//设置角色在河岸上的位置
public void setFromPos(Vector3 vec)
{
obj.transform.position = vec;
PosFrom = vec;
}
public void setToPos(Vector3 vec)
{
PosTo = vec;
}
//点击河岸上的对象
//将角色移动至船上
public void MoveToBoat(int i)
{
position = 2;
if (i == 1){
boatPos = 0;
obj.transform.position = new Vector3(0, -3, 4);
}
else if(i == 2){
boatPos = 1;
obj.transform.position = new Vector3(0, -3, 6);
}
else if(i == 3){
boatPos = 1;
obj.transform.position = new Vector3(0, -3, -4);
}
else{
boatPos = 0;
obj.transform.position = new Vector3(0, -3, -6);
}
}
//点击船上的对象
//将角色移动回河岸
public void MoveGroundFrom()
{
obj.transform.position = PosFrom;
position = 0;
}
public void MoveGroundTo()
{
obj.transform.position = PosTo;
position = 1;
}
public void setBoatPos(int i)
{
boatPos = i;
}
public void setPos(int i)
{
position = i;
}
public Vector3 getGroundPos(int i)
{
if(i == 0){
return PosFrom;
} else {
return PosTo;
}
}
}
代码地址
代码转送门链接: link