Unity空间与运动(中山大学3D游戏作业3)

Unity空间与运动(中山大学3D游戏作业3)

代码仓库:https://github.com/linfn3/3d_game

b站视频:https://www.bilibili.com/video/BV1YD4y1r7GR/?vd_source=6d44ed4eff5157be7cd6838983f17b44

一、程序验证

物体运动的本质

unity中物体运动的本质是游戏对象的位置和状态变化。

三种方法实现抛物线运动

  1. 使用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;
	}
}
  1. 将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;
    }
}

  1. 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模式:
在这里插入图片描述

  1. model包括Boat、Character和Stone,用于维护船、角色(恶魔、牧师)、石头的坐标、特征位置等,具体代码过多,详细可见代码仓库。
  2. 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);
            }
        }
    }
}

  1. 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:被唤醒时自动导入场景、实现判断游戏胜负等功能。
以上代码详见仓库:

展示:
胜利:
在这里插入图片描述
失败:
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值