unity3d跑酷游戏论文,unity3d跑酷游戏制作教程
终极管理员 知识笔记 129阅读
finish_all
* 方块跑酷 1.教程链接翻墙 2.基础制作

最终成果
2.1 基本场景1.创建Cube作为跑道

1记得把位置Reset
2改名为ground
3改变其参数x15y1z100
4调整位置使其从相机同一位置开始。
选中单击f可聚焦。
2.创建Cube作为Player
1Reset重置位置
2移动Cube将其在跑道上
3改名为Player。
3.上色
1创建文件夹Material并在文件夹里创建Player的材料体PlayerMat
2将选择好颜色的材料体拖至Player。
4.为Player加上组件Rigidbody
1在Player的Add Component中选择Rigidbody并添加
2适当抬高Player位置并运行发现Player有重力效果。
5.设置SkyBox
1改为纯色Skybox
2改颜色
6.保存场景并把当前场景改名为Level1。
2.2 编写程序1.创建Scripts文件夹并在文件夹里创建C#脚本命名为Player Movement将脚本拖入Player中。
2.编写Player Movement的脚本
using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerMovement : MonoBehaviour{ public Rigidbody rb;//定义刚体 // 用Fixed稀释重力 void FixedUpdate() { rb.AddForce(0,0,2000*Time.deltaTime);//在z轴上添加2000的力 }}
3.将Player中的Rigidbody拖入脚本的rb中。
2.3 方块移动1.创建Physic Materia模拟摩擦力并将其命名Slippery拖至场景中的Ground。
2.更新PlayerMovement脚本为Player增加向前的力。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerMovement : MonoBehaviour{ public Rigidbody rb;//定义刚体 public float forwardForce 2000f;//定义向前的力 // 用Fixed稀释重力 void FixedUpdate() { rb.AddForce(0,0, forwardForce*Time.deltaTime);//在z轴上添加2000的力 }}
物体开始平滑向前运动
using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerMovement : MonoBehaviour{ public Rigidbody rb;//定义刚体 public float forwardForce 1000f;//定义向前的力 public float sidewaysForce 200f;//定义侧向力 // 用Fixed稀释重力 void FixedUpdate() { rb.AddForce(0,0, forwardForce*Time.deltaTime);//在z轴上添加力 //按d键向右移动 if (Input.GetKey(d)) { rb.AddForce(sidewaysForce*Time.deltaTime, 0, 0);//往x轴正方向添加力 } //按a键向左移动 if (Input.GetKey(a)) { rb.AddForce(-sidewaysForce*Time.deltaTime, 0, 0);//往x轴负方向添加力 } }}2.4 摄像跟随
注若单纯把摄像机作为Player的子集当Player碰撞时Player旋转什么的场景会出错。所以通过编写摄像机脚本控制。
1.在Scripts文件夹中创建C#脚本并命名为FollowPlayer拖入Camera组件中。
2.编写FollowPlayer脚本
using UnityEngine;public class FollowPlayer : MonoBehaviour{ public Transform player;//定义变化的物体 public Vector3 offset; // Update is called once per frame void Update() { transform.position player.position offset; }}
3.在Camera的Inspector中改变offset的值y1z-5。
2.5 碰撞处理1.添加障碍物
1新键Cube并命名为Barrier
2添加素材改变其颜色
3改变大小使x2
4添加刚体组件并把Mass改为2确保比Player重。
5为障碍物设置标签barrier。
2.创建C#脚本命名为Player Collision并作用在Player上。
3.编写Player Collision脚本。
using UnityEngine;public class PlayerCollision : MonoBehaviour{ public PlayerMovement movement;//存放移动脚本 void OnCollisionEnter(Collision collisionInfo) { //如果撞到了障碍物 if(collisionInfo.collider.tagObstacle) { movement.enabledfalse;//保证碰撞之后不再移动 } }}
并将Player Movement脚本拖入Player Collision的movement卡槽中
2.6 游戏设计1.将Barrier拖入Material文件夹中作为一个预制体这样要设置障碍物时直接把预制体拖入即可。
2.改变Ground大小Scale的x10000Position的z4980使其形成一个无尽平面。
3.在Lighting中改善场景
1设置SkyBox
2并开启fox使玩家看不到远处的障碍物。
2.7 游戏结束1.新建C#脚本命名为GameManager创建空组件Game Manager并把脚本拖入。
2.编写GameManager脚本。
using UnityEngine;public class GameManager : MonoBehaviour{ public void EndGame() { Debug.Log(GAME OVER); }}
3.将Player作为预制体
把Player拖入Asset中作为预制体把原来的Player删掉再把预制体Player拖入场景中。
4.修改PlayerCollision脚本
碰撞后游戏结束。
using UnityEngine;public class PlayerCollision : MonoBehaviour{ public PlayerMovement movement;//存放移动脚本 void OnCollisionEnter(Collision collisionInfo) { //如果撞到了障碍物 if(collisionInfo.collider.tagBarrier) { movement.enabledfalse;//保证碰撞之后不再移动 FindObjectOfType<GameManager>().EndGame();//结束游戏 } }}
5.修改PlayerMovement脚本
掉下边缘后游戏结束。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerMovement : MonoBehaviour{ public Rigidbody rb;//定义刚体 public float forwardForce 1000f;//定义向前的力 public float sidewaysForce 200f;//定义侧向力 // 用Fixed稀释重力 void FixedUpdate() { rb.AddForce(0,0, forwardForce*Time.deltaTime);//在z轴上添加力 //按d键向右移动 if (Input.GetKey(d)) { rb.AddForce(sidewaysForce*Time.deltaTime, 0, 0);//往x轴正方向添加力 } //按a键向左移动 if (Input.GetKey(a)) { rb.AddForce(-sidewaysForce*Time.deltaTime, 0, 0);//往x轴负方向添加力 } //Player掉下边缘时游戏结束 if(rb.position.y<-1f) { FindObjectOfType<GameManager>().EndGame(); } }}2.8 获胜界面
1.胜利的触发器。
1创建一个Cube并重命名为End作为触发器。
2将其拖至赛道随后并设置大小使其能覆盖整个赛道宽。
3取消勾选Mesh Renderer使其在场景中不可见。
4为了在编辑中可见点击Inspector中的图形图像选择绿色标签。
2.编写触发器脚本
1创建C#脚本并命名为EndTrigger拖至End。
2修改GameManager脚本
using UnityEngine;using UnityEngine.SceneManagement;public class GameManager : MonoBehaviour{ bool gameHasEnded false;//判断游戏是否结束 public float restartDelay 1f;//复活延迟 //游戏胜利时 public void CompleteLevel() { Debug.Log(LEVEL WON!); } public void EndGame() { if (gameHasEndedfalse) { gameHasEnded true; Debug.Log(GAME OVER); Invoke(Restart, restartDelay);//重启游戏,必有复活延迟 } } void Restart() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); }}
3编写EndTrigger脚本
using UnityEngine;public class EndTrigger : MonoBehaviour{ public GameManager gameManager; void OnTriggerEnter() { gameManager.CompleteLevel(); }}
4将GameManager拖入EndTrigger的新建卡槽中。
3.UI设计获胜界面
创建UI界面的Panel和Text达到以下效果
4.创建动画
1Window--->Animation--->Create--->创建新文件夹Animation--->保存动画并命名为LevelComplete
2录制动画并形成渐变效果
5.场景跳转
1在LevelComplete上添加脚本LevelComplete
using UnityEngine;using UnityEngine.SceneManagement;public class LevelComplete : MonoBehaviour{ public void LoadNextLevel() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex1); }}
2在动画中成功界面两秒之后edd event并添加函数LoadNextLevel()实现场景转换。
2.9 结束界面1.新建界面
file--->New Scene
2.在新界面创建UI的panel并把它调为浅灰色不透明
3.新键Text文本实现以下效果
4.在UI界面创建一个Button
1添加shadow组件将x0y-2并调整透明度
2创建Credits空物体并把Credits脚本作用在上面放在button本身也可以后序将button拖入卡槽编写脚本
using UnityEngine;public class Credits : MonoBehaviour{ public void Quit() { Debug.Log(Quit); Application.Quit();//只有导出时会实现 }}
3在button的on click属性里点加号并将物体credits拖入再选择函数Quit<img srcD:\Document\Typora\Game.assets\image-20231001000420760.png altimage-20231001000420760 stylezoom:25%; />
4将界面保存并命名为Credits。
2.10 开始菜单1.复制粘贴结束界面Credits并重命名为Menu。
2.修改界面如下
3.编写start脚本
using UnityEngine.SceneManagement;using UnityEngine;public class Menu : MonoBehaviour{ public void StartGame() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex1); }}