This sample shows how to set up a simple character controller using the input system. As there is more than one way to do it, the sample illustrates several ways. Each demonstration is set up as a separate scene. The basic functionality in all the scenes is the same. You can move and look around and fire projectiles (colored cubes) into the scene. In some scenes, only gamepads are supported but the more involved demonstrations support several different inputs concurrently.
# SimpleDemo_UsingState
[Source](./SimpleController_UsingState.cs)
This starts off at the lowest level by demonstrating how to wire up input by polling input state directly in a `MonoBehaviour.Update` function. For simplicity's sake it only deals with gamepads but the same mechanism works in equivalent ways for other types of input devices (e.g. using `Mouse.current` and `Keyboard.current`).
The key APIs demonstrated here are `Gamepad.current` and `InputControl.ReadValue`.
```CSharp
public class SimpleController_UsingState : MonoBehaviour
{
//...
public void Update()
{
var gamepad = Gamepad.current;
if (gamepad == null)
return;
var move = Gamepad.leftStick.ReadValue();
//...
}
}
```
# SimpleDemo_UsingActions
[Source](./SimpleController_UsingActions.cs)
This moves one level higher and moves input over to "input actions". These are input abstractions that allow you to bind to input sources indirectly.
In this scene, the actions are embedded directly into the character controller component. This allows setting up the bindings for the actions directly in the inspector. To see the actions and their bindings, select the `Player` object in the hierarchy and look at the `SimpleController_UsingActions` component in the inspector.
The key APIs demonstrated here are `InputAction` and its `Enable`/`Disable` methods and its `ReadValue` method.
```CSharp
public class SimpleController_UsingActions : MonoBehaviour
{
public InputAction moveAction;
//...
public void OnEnable()
{
moveAction.Enable();
//...
}
public void OnDisable()
{
moveAction.Disable();
//...
}
public void Update()
{
var move = moveAction.ReadValue<Vector2>();
//...
}
}
```
The sample also demonstrates how to use a `Tap` and a `SlowTap` interaction on the fire action to implement a charged shooting mechanism. Note that in this case, we run the firing logic right from within the action using the action's `started`, `performed`, and `canceled` callbacks.
```CSharp
fireAction.performed +=
ctx =>
{
if (ctx.interaction is SlowTapInteraction)
{
StartCoroutine(BurstFire((int)(ctx.duration * burstSpeed)));
}
else
{
Fire();
}
m_Charging = false;
};
fireAction.started +=
ctx =>
{
if (ctx.interaction is SlowTapInteraction)
m_Charging = true;
};
fireAction.canceled +=
ctx =>
{
m_Charging = false;
};
```
# SimpleDemo_UsingActionAsset
[Source](./SimpleController_UsingActionAsset.cs)
As more and more actions are added, it can become quite tedious to manually set up and `Enable` and `Disable` all the actions. We could use an `InputActionMap` in the component like so
```CSharp
public class SimpleController : MonoBehaviour
{
public InputActionMap actions;
public void OnEnable()
{
actions.Enable();
}
public void OnDisable()
{
actions.Disable();
}
}
```
but then we would have to look up all the actions manually in the action map. A simpler approach is to put all our actions in a separate asset and generate a C# wrapper class that automatically performs the lookup for us.
To create such an `.inputactions` asset, right-click in the Project Browser and click `Create >> Input Actions`. To edit the actions, double-click the `.inputactions` asset and a separate window will come up. The asset we use in this example is [SimpleControls.inputactions](SimpleControls.inputactions).
When you select the asset, note that `Generate C# Class` is ticked in the import settings. This triggers the generation of [SimpleControls.cs](SimpleControls.cs) based on the `.inputactions` file.
Regarding the `SimpleController_UsingActionAsset` script, there are some notable differences.
```CSharp
public class SimpleController_UsingActionAsset
{
// This replaces the InputAction instances we had before with
// the generated C# class.
private SimpleControls m_Controls;
//...
public void Awake()
{
// To use the controls, we need to instantiate them.
// This can be done arbitrary many times. E.g. there
// can be multiple players each with its own SimpleControls
// instance.
m_Controls = new SimpleControls();
// The generated C# class exposes all the action map
// and actions in the asset by name. Here, we reference
// the `fire` action in the `gameplay` action map, for
// example.
m_Controls.gameplay.fire.performed +=
//...
}
//...
public void Update()
{
// Same here, we can just look the actions up by name.
var look = m_Controls.gameplay.look.ReadValue<Vector2>();
var move = m_Controls.gameplay.move.ReadValue<Vector2>();
//...
}
}
```
Just for kicks, this sample also adds keyboard and mouse control to the game.
# SimpleDemo_UsingPlayerInput
[Source](./SimpleController_UsingPlayerInput.cs)
Finally, we reached the highest level of the input system. While scripting input like in the examples above can be quick and easy, it becomes hard to manage when there can be multiple devices and/or multiple players in the game. This is where `PlayerInput` comes in.
`PlayerInput` automatically manages per-player device assignments and can also automatically handle control scheme switching in single player (e.g. when the player switches between a gamepad and mouse&keyboard).
In our case, we're not getting too much out of it since we don't have control schemes or multiple players but still, let's have a look.
The first thing you'll probably notice is that now there are two script components on the `Player` object, one being the usual `SimpleController` and the other being `PlayerInput`. The latter is what now refers to [SimpleControls.inputactions](SimpleControls.inputactions). It also has `gameplay` set as the `Default Action Map` so that the gameplay actions will get enabled right away when `PlayerInput` itself is enabled.
For getting callbacks, we have chosen `Invoke Unity Events` as the `Behavior`. If you expand the `Events` foldout in the inspector, you can see that `OnFire`, `OnMove`, and `OnLook` are added to the respective events. Each callback method here looks like the `started`, `performed`, and `canceled` callbacks we've already seen on `fireAction` before.
```CSharp
public class SimpleController_UsingPlayerInput : MonoBehaviour
{
private Vector2 m_Move;
//...
public void OnMove(InputAction.CallbackContext context)
{
m_Move = context.ReadValue<Vector2>();
}
//...
}
```
Unity游戏项目-3D迷宫(免费)
需积分: 0 44 浏览量
更新于2023-04-24
26
收藏 599.42MB ZIP 举报
Unity是全球广受欢迎的游戏开发引擎,它以其强大的3D和2D渲染能力、直观的可视化编辑界面以及跨平台支持而闻名。在这个“Unity游戏项目-3D迷宫”中,我们将会探讨Unity在构建3D游戏,特别是迷宫类游戏方面的应用。
3D迷宫的设计通常涉及到以下几个关键知识点:
1. **3D建模**:在Unity中,我们可以使用内置的简单3D形状,如立方体、球体等,或者导入外部的3D模型来构建迷宫的墙壁、起点和终点。建模工具如Blender或3DS Max可以用于创建更复杂的自定义迷宫结构。
2. **导航网格(NavMesh)**:Unity的导航网格系统是构建迷宫游戏的核心部分,它允许AI角色(例如玩家控制的角色)自动寻找路径。通过设置NavMeshAgent,我们可以让角色在迷宫中自由移动,并自动避开障碍物。
3. **光照与材质**:Unity提供了强大的光照系统,包括环境光、聚光灯和点光源等,可以为迷宫营造出不同的氛围。同时,使用不同的材质和纹理可以增加迷宫的视觉效果,使其看起来更加真实。
4. **脚本编程**:使用C#编写Unity脚本,可以实现游戏逻辑,如检查玩家是否到达终点、迷宫生成算法、游戏计时器、玩家输入控制等功能。例如,一个简单的迷宫生成算法可能涉及随机放置墙壁以创建复杂的迷宫结构。
5. **用户交互**:Unity支持键盘、鼠标、手柄等多种输入方式,可以轻松实现玩家在迷宫中的移动、视角转换等操作。此外,还可以添加UI元素,如得分显示、提示信息等,提升用户体验。
6. **场景管理**:在大型迷宫项目中,可能需要将迷宫分割成多个小区域,以优化性能。Unity的场景管理功能允许你按需加载和卸载不同部分,避免一次性加载整个迷宫导致的性能问题。
7. **打包和发布**:完成项目后,Unity可以方便地将游戏打包为适用于Windows、Mac、Linux等多平台的可执行文件。在这个项目中,提供的打包后的PC端游戏表明开发者已经完成了这一过程。
8. **资源优化**:为了确保游戏在各种设备上都能流畅运行,需要对3D模型、纹理和脚本进行优化,如降低模型多边形数、压缩纹理质量和使用懒加载技术等。
通过学习和实践这个3D迷宫项目,开发者不仅可以掌握Unity的基本操作,还能深入理解游戏逻辑设计、性能优化以及跨平台发布等多个重要环节,对于提高Unity游戏开发技能大有裨益。无论是新手还是经验丰富的开发者,都能从中受益。
@黑树叶
- 粉丝: 10
- 资源: 3
最新资源
- 机器人研究数模实验方案LQR控制,m函数调用simulink中的控制算法,在simscape Multibody中的物理模型中测试 跑通模型花了很多时间,这个能帮你节省下时间 方法适合所有机器人研
- 2023华数杯C题总结PDF
- 国内可用AI工具合集(更新到20241130)
- P2轿车并联模型,完全基于正向开发,先计算整车阻力,然后根据当前车辆模式进行扭矩分配
- 混合储能系统能量管理simulink仿真模型 蓄电池和超级电容构成的混合储能系统能量管理控制策略 采用lpf分配系统功率,放电过程中超级电容soc限值管理策略,soc高时多放电,小时少放电
- 三相整流仿真,电压外环电流内环双闭环pi控制,svpwm,pll锁相环,整流电压稳定在650v 功率等级可修改 当前为10kw 如需其他功率等级,与对应修改系统及控制参数 单相整流仿真,双闭环控制 有
- 无监督分类算法代码.zip
- 基于西门子S7-200 PLC和组态王组态3泵恒压供水系统控制
- xilinx FPGA srio 接口verilog源码程序,顶层接口封装为fifo,使用简单方便,已运用在实际项目上 本源码支持srio NWRITE、NWRITE-R、SWRITE、MAINTE
- 导轨搬运设备程序,包含三菱PLC以及信捷HMI源程序,程序包含注释,可附带电气接线图 PLC型号FX3U,人机型号:TH765 可用于各种搬运码垛设备开发参照套用,也可以作为工控爱者学习参考 不包含硬
- Python基础题库100题及答案PDF
- 通过Matlab Simulink仿真平台,以IEEE10节点配电系统为背景,通过仿真分析风机和光伏接入位置和容量对电压分布和波动的影响 包含word说明文档
- vofa+-1.3.12-x64-installer
- 1200W开关电源原理图及PCB文件
- TMC5160 步进驱动器例程
- HTML5实现好看的儿童特长培训班网页源码.zip