游戏对象运动本质就是在每一帧改变游戏对象的空间属性(位置、欧拉角、比例),并展现出来。
(1)修改Transform属性:利用transform改变position来实现抛物线运动,根据物理的运动学知识,只要水平方向的移动速度是不变的,竖直方向有一定的加速度变化的,两个方向的运动矢量相加便是抛物线运动。具体实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class movement1 : MonoBehaviour
{public float speed;// Start is called before the first frame updatevoid Start(){speed = 0;}// Update is called once per framevoid Update(){ansform.position += Vector3.left * Time.deltaTime * ansform.position += speed / 10 * Vector3.down * Time.deltaTime;++speed;}
}
(2)使用向量Vector3:定义一个向量Vector3,同时定义该变量的值,其也是竖直方向上是一个均匀增加的数值,水平方向是一个保持不变的数值,然后将游戏对象原本的position属性与该向量相加。具体实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class movement2 : MonoBehaviour
{public float speed;// Start is called before the first frame updatevoid Start(){speed = 0;}// Update is called once per framevoid Update(){Vector3 vec1= new Vector3(-Time.deltaTime * 4, -Time.deltaTime * (speed / 10), 0);++ansform.position += vec1;}
}
(3)利用transform的Translate函数:传入一个Vector3变量来改变position,该变量做抛物线运动。具体实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class movement3 : MonoBehaviour
{public float speed;// Start is called before the first frame updatevoid Start(){speed = 0;}// Update is called once per framevoid Update(){Vector3 vec1= new Vector3(-Time.deltaTime * 4, -Time.deltaTime * (speed / 10), 0);++speed;transform.Translate(vec1);}
}
(1)下载太阳系贴图:
下载链接
(2)设置相对位置和贴图:
(3)实现自转和公转脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class move : MonoBehaviour
{public Transform center; //各天体公转的圆心public float globalSpeed; //公转速度public float selfSpeed; //自转速度public float ry, rz; //公转平面所在的轴// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){Vector3 axis = new Vector3(0, ry, rz); //公转轴ansform.RotateAround(center.position, axis, globalSpeed * Time.deltaTime); //公转ansform.Rotate(Vector3.up * selfSpeed * Time.deltaTime); //自转}
}
通过设置公转轴使得不在同一个法平面公转,调整速度使得公转速度不一致。
阅读以下游戏脚本
Priests and Devils
Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many > ways. Keep all priests alive! Good luck!
程序需要满足的要求:
play the game ( .html )
牧师,恶魔,船,河,两岸
动作 | 条件 |
---|---|
开船 | 有人物在上面 |
上船 | 船在对应边且船上有位置 (船只有俩个位置) |
下船 | 船在对应边 |
杀人 | 在船上时牧师人数不大于恶魔或者岸边时牧师人数小于恶魔 |
预制对象的使用:参考官方文档
使用 C# 集合类型 有效组织对象
编程要求:整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
请使用课件架构图编程,不接受非 MVC 结构程序
注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using mygame;public class Model : MonoBehaviour
{Stack<GameObject> start_priests = new Stack<GameObject>();Stack<GameObject> end_priests = new Stack<GameObject>();Stack<GameObject> start_devils = new Stack<GameObject>();Stack<GameObject> end_devils = new Stack<GameObject>();GameObject[] boat = new GameObject[2];GameObject boat_obj;public float speed = 50;SSDirector one;//对象的位置Vector3 boatStartPos = new Vector3(-2, 0.25f, 0);Vector3 boatEndPos = new Vector3(2, 0.25f, 0);Vector3 sideStartPos = new Vector3(-5, 0, 0);Vector3 sideEndPos = new Vector3(5, 0, 0);Vector3 riverPos = new Vector3(0, -0.25f, 0); float gap = 1.1f;Vector3 priestsStartPos = new Vector3(-3.25f, 0.75f, 0);Vector3 priestsEndPos = new Vector3(3.25f, 0.75f, 0);Vector3 devilsStartPos = new Vector3(-5, 1, 0);Vector3 devilsEndPos = new Vector3(5, 1, 0);// Start is called before the first frame updatevoid Start(){one = SSDirector.GetInstance();one.setModel(this);loadSrc();}// Update is called once per framevoid Update(){setpositionS(start_priests, priestsStartPos);setpositionE(end_priests, priestsEndPos);setpositionS(start_devils, devilsStartPos);setpositionE(end_devils, devilsEndPos);if (one.state == State.SE_move){ansform.position = Vector3.MoveTowards(ansform.position, boatEndPos, Time.deltaTime * speed);if (ansform.position == boatEndPos){one.state = State.End;}checkD();}else if (one.state == State.ES_move){ansform.position = Vector3.MoveTowards(ansform.position, boatStartPos, Time.deltaTime * speed);if (ansform.position == boatStartPos){one.state = State.Start;}checkD();}checkV();}//加载游戏对象void loadSrc(){//sidesInstantiate(Resources.Load("side"), sideStartPos, Quaternion.identity);Instantiate(Resources.Load("side"), sideEndPos, Quaternion.identity);//riverInstantiate(Resources.Load("river"), riverPos, Quaternion.identity);//boatboat_obj = Instantiate(Resources.Load("boat"), boatStartPos, Quaternion.identity) as GameObject;//prisets and devilsfor (int i = 0; i < 3; i++){start_priests.Push(Instantiate(Resources.Load("Priest")) as GameObject);start_devils.Push(Instantiate(Resources.Load("Devil")) as GameObject);}}//设置起点对象位置void setpositionS(Stack<GameObject> aaa, Vector3 pos){GameObject[] temp = aaa.ToArray();for (int i = 0; i < aaa.Count; i++){temp[i].transform.position = pos + new Vector3(-gap * i * 0.5f, 0, 0);}}//设置终点对象位置void setpositionE(Stack<GameObject> aaa, Vector3 pos){GameObject[] temp = aaa.ToArray();for (int i = 0; i < aaa.Count; i++){temp[i].transform.position = pos + new Vector3(gap * i * 0.5f, 0, 0);}}//上船void getOnTheBoat(GameObject obj){ansform.parent = ansform;if (boatNum() != 0){if (boat[0] == null){boat[0] = ansform.position = ansform.position + new Vector3(0.3f, ansform.position.y - 0.25f, 0);}else{boat[1] = ansform.position = ansform.position + new Vector3(-0.3f, ansform.position.y - 0.25f, 0);}}}//判断船上的空位数int boatNum(){int num = 0;for (int i = 0; i < 2; i++){if (boat[i] == null){++ num;}}return num;}//船移动public void moveBoat(){if (boatNum() != 2){if (one.state == State.Start){one.state = State.SE_move;}else if (one.state == State.End){one.state = State.ES_move;}}}//下船public void getOffTheBoat(int flag){if (flag == 0 || flag == 2){for (int i = 0; i < 2; ++i){if (boat[i] != null && boat[i].tag == "Priest"){Debug.Log("enter1");if (flag == 0){start_priests.Push(boat[i]);}else{end_priests.Push(boat[i]);}boat[i] = null;break;}}}else if (flag == 1 || flag == 3){for (int i = 0; i < 2; ++i){if (boat[i] != null && boat[i].tag == "Devil"){if (flag == 1){start_devils.Push(boat[i]);}else{end_devils.Push(boat[i]);}boat[i] = null;break;}}}else{for (int i = 0; i < 2; ++i){if (boat[i] != null){ if (boat[i].tag == "Devil"){start_devils.Push(boat[i]);}elsestart_priests.Push(boat[i]);boat[i] = null;}}}}//检查是否失败void checkD(){int bp = 0, bd = 0;for (int i = 0; i < 2; i++){if (boat[i] != null && boat[i].tag == "Priest"){bp++;}else if (boat[i] != null && boat[i].tag == "Devil"){bd++;}}int sp = start_priests.Count, sd = start_devils.Count, ep = end_priests.Count, ed = end_devils.Count;if (one.state == State.Start){int sum = sp + bp;if ((sum != 0 && sum < (sd + bd)) || (ep != 0 && ep < ed))one.state = State.Lose;}else{int sum = ep + bp;if ((sp != 0 && sp < sd) || (sum != 0 && sum < ed + bd))one.state = State.Lose;}}//检查是否胜利void checkV(){if (end_devils.Count == 3 && end_priests.Count == 3){one.state = State.Win;return;}}//游戏对象从岸上到船上的变化public void priS(){if (start_priests.Count != 0 && boatNum() != 0 && one.state == State.Start){getOnTheBoat(start_priests.Pop());}}public void priE(){if (end_priests.Count != 0 && boatNum() != 0 && one.state == State.End){getOnTheBoat(end_priests.Pop());}}public void delS(){if (start_devils.Count != 0 && boatNum() != 0 && one.state == State.Start){getOnTheBoat(start_devils.Pop());}}public void delE(){if (end_devils.Count != 0 && boatNum() != 0 && one.state == State.End){getOnTheBoat(end_devils.Pop());}}//重置游戏public void Reset(){ansform.position = boatStartPos;one.state = State.Start;int num1 = end_devils.Count, num2 = end_priests.Count;for (int i = 0; i < num1; i++){Debug.Log(i);start_devils.Push(end_devils.Pop());}for (int i = 0; i < num2; i++){start_priests.Push(end_priests.Pop());}if (boatNum() != 2){getOffTheBoat(4);}}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using mygame;public class View : MonoBehaviour
{SSDirector one;UserAction action;// Start is called before the first frame updatevoid Start(){one = SSDirector.GetInstance();action = SSDirector.GetInstance() as UserAction;}private void OnGUI(){GUI.skin.label.fontSize = 30;if (one.state == State.Win){if (GUI.Button(new Rect(650, 140, 300, 100), "WINn(click here to reset)")){set();}}if (one.state == State.Lose){if (GUI.Button(new Rect(650, 140, 300, 100), "LOSEn(click here to reset)")){set();}}if (GUI.Button(new Rect(700, 80, 100, 50), "GO")){veBoat();}if (GUI.Button(new Rect(550, 250, 90, 50), "LPriestsOFF")){action.offBoatLP();}if (GUI.Button(new Rect(950, 250, 90, 50), "RPriestsOFF")){action.offBoatRP();}if (GUI.Button(new Rect(650, 250, 90, 50), "LDevilsOFF")){action.offBoatLD();}if (GUI.Button(new Rect(850, 250, 90, 50), "RDevilsOFF")){action.offBoatRD();}if (GUI.Button(new Rect(350, 130, 75, 50), "PriestsON")){action.priestSOnSide();}if (GUI.Button(new Rect(500, 130, 75, 50), "DevilsON")){action.devilSOnSide();}if (GUI.Button(new Rect(1050, 130, 75, 50), "DevilsON")){action.devilEOnSide();}if (GUI.Button(new Rect(1200, 130, 75, 50), "PriestsON")){action.priestEOnSide();}}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using mygame;namespace mygame
{public enum State {Start, SE_move, ES_move, End, Win, Lose };public interface UserAction{void priestSOnSide();void priestEOnSide();void devilSOnSide();void devilEOnSide();void moveBoat();void offBoatLP();void offBoatRP();void offBoatLD();void offBoatRD();void reset();}public class SSDirector : System.Object, UserAction{private static SSDirector _instance;public Controller currentScenceController;public State state = State.Start;private Model game_obj;public static SSDirector GetInstance(){if (_instance == null){_instance = new SSDirector();}return _instance;}public Model getModel(){return game_obj;}internal void setModel(Model someone){if (game_obj == null){game_obj = someone;}}public void priestSOnSide(){game_obj.priS();}public void priestEOnSide(){game_obj.priE();}public void devilSOnSide(){game_obj.delS();}public void devilEOnSide(){game_obj.delE();}public void moveBoat(){veBoat();}public void offBoatLP(){OffTheBoat(0);}public void offBoatRP(){OffTheBoat(2);}public void offBoatLD(){OffTheBoat(1);}public void offBoatRD(){OffTheBoat(3);}public void reset(){game_obj.Reset();}}
}public class Controller : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){SSDirector one = SSDirector.GetInstance();}// Update is called once per framevoid Update(){}
}
最终效果:
视频链接
本文发布于:2024-01-27 18:56:47,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063530092018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |