Unity空间与运动(中山大学3D游戏作业3)
代码仓库:https://github.com/linfn3/3d_game
b站视频:https://www.bilibili.com/video/BV1YD4y1r7GR/?vd_source=6d44ed4eff5157be7cd6838983f17b44
一、程序验证
物体运动的本质
unity中物体运动的本质是游戏对象的位置和状态变化。
三种方法实现抛物线运动
- 使用translate方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private float speed1 = 0.00f;
public float speed2 = 5.00f;
const float g = 0.1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 change = new Vector3(Time.deltaTime * speed2, -Time.deltaTime * speed1, 0.0f);
this.transform.Translate(change);
speed1 += g;
}
}
- 将transfrom.position1加上改变向量
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Behavior : MonoBehaviour
{
private float speed1 = 0.00f;
public float speed2 = 5.00f;
const float g = 0.1f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 change = new Vector3(Time.deltaTime * speed2, -Time.deltaTime * speed1, 0.0f);
this.transform.position += change;
speed1 += g;
}
}
- position加减实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Behavior : MonoBehaviour
{
private float speed1 = 0.00f;
public float speed2 = 5.00f;
const float g = 0.098f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.position += Vector3.right * Time.deltaTime * speed2;
this.transform.position += Vector3.down * Time.deltaTime * speed1;
speed1 += g;
}
}
实现太阳系
首先,9个创建球体最为星体,再加上图片作为材质:
然后编写代码,调用transform.RotateAround方法,使得行星绕着太阳转。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GameObject.Find("global").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 30 * Time.deltaTime);
GameObject.Find("global").transform.Rotate(Vector3.up * Time.deltaTime*10000);
GameObject.Find("Mercury").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 20 * Time.deltaTime);
GameObject.Find("Mercury").transform.Rotate(Vector3.up * Time.deltaTime * 10000);
GameObject.Find("gold").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 45 * Time.deltaTime);
GameObject.Find("gold").transform.Rotate(Vector3.up * Time.deltaTime * 10000);
GameObject.Find("juiter").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 35 * Time.deltaTime);
GameObject.Find("juiter").transform.Rotate(Vector3.up * Time.deltaTime * 10000);
GameObject.Find("Saturn").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 50 * Time.deltaTime);
GameObject.Find("Saturn").transform.Rotate(Vector3.up * Time.deltaTime * 10000);
GameObject.Find("spark").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 15 * Time.deltaTime);
GameObject.Find("spark").transform.Rotate(Vector3.up * Time.deltaTime * 10000);
GameObject.Find("sky").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 25 * Time.deltaTime);
GameObject.Find("sky").transform.Rotate(Vector3.up * Time.deltaTime * 10000);
GameObject.Find("sea").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 15 * Time.deltaTime);
GameObject.Find("sea").transform.Rotate(Vector3.up * Time.deltaTime * 10000);
}
}
运行游戏,查看结果:
二、牧师与恶魔游戏
要求:
事务表:
事务 | 数量 |
---|---|
牧师 | 3 |
恶魔 | 3 |
河流 | 1 |
石头(河岸) | 2 |
船 | 1 |
动作表:
动作 | 事件 |
---|---|
点击小船 | 船移动 |
点击角色 | 角色上(下)船 |
预制:
如下图,用红色方块代表恶魔,用球体代表牧师。
MVC模式:
- model包括Boat、Character和Stone,用于维护船、角色(恶魔、牧师)、石头的坐标、特征位置等,具体代码过多,详细可见代码仓库。
- UserGUI用于绘制场景和获取鼠标事件:
void OnMouseDown()获取鼠标事件
OnGUI() 进行渲染绘制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MyNamespace
{
public class UserGUI : MonoBehaviour
{
private IUserAction action;
private GUIStyle textStyle;
public CharacterController contronller;
public int flag;
private GUIStyle hintStyle;
private GUIStyle btnStyle;
// 初始化获得当前场记 CurrentSecnController
void Start()
{
flag = 0;
action = Director.GetInstance().CurrentSecnController as IUserAction;
}
// Update is called once per frame
void OnGUI()
{
textStyle = new GUIStyle
{
fontSize = 40,
alignment = TextAnchor.MiddleCenter
};
hintStyle = new GUIStyle
{
fontSize = 15,
fontStyle = FontStyle.Normal
};
btnStyle = new GUIStyle("button")
{
fontSize = 30
};
if (flag == 1)
{
// Lose
GUI.Label(new Rect(Screen.width / 2 - 48, Screen.height / 2 - 85, 100, 50), "Lose!", textStyle);
if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2, 140, 70), "Restart", btnStyle))
{
flag = 0;
action.Restart();
}
}
else if (flag == 2)
{
// Win
GUI.Label(new Rect(Screen.width / 2 - 48, Screen.height / 2 - 85, 100, 50), "Win!", textStyle);
if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2, 140, 70), "Restart", btnStyle))
{
flag = 0;
action.Restart();
}
}
}
public void SetCharacterCtrl(CharacterController controller)
{
contronller = controller;
}
void OnMouseDown()
{
Debug.Log("tmp");
if (gameObject.name == "boat")
{
action.Boatmoved();
Debug.Log("yes");
}
else
{
Debug.Log("r");
action.CharacterClicked(contronller);
}
}
}
}
- Controller包括Director、FirstController、GameObjectController、Moveable、interFaces
Director:用于获取游戏场景,继承System.Object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyNamespace;
namespace MyNamespace
{
public class Director : System.Object
{
private static Director SSDirector;
public ISceneController CurrentSecnController { get; set; }
// get instance anytime anywhere!
public static Director GetInstance()
{
if (SSDirector == null)
{
SSDirector = new Director();
}
return SSDirector;
}
public int getFPS()
{
return Application.targetFrameRate;
}
public void setFPS(int fps)
{
Application.targetFrameRate = fps;
}
}
}
Moveable:控制游戏对象运动
GameObjectController:控制上船、上岸、计算穿上的角色数量、计算岸上和船上空余位置等功能
FirstController:被唤醒时自动导入场景、实现判断游戏胜负等功能。
以上代码详见仓库:文章来源:https://www.toymoban.com/news/detail-437402.html
展示:
胜利:
失败:
文章来源地址https://www.toymoban.com/news/detail-437402.html
到了这里,关于Unity空间与运动(中山大学3D游戏作业3)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!