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>();
}
//...
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Cyber Hunter 量子特攻2d横版像素射击游戏项目源码C# 支持Unity版本2019.3.13f1或更高 我的第一個遊戲是用 Unity 2D 製作的。一款科幻平台遊戲,有很多關卡和不同的 Boss 可供擊敗 解决具有挑战性的谜题并解锁多个关卡! 简单的控制让你超级上瘾。
资源推荐
资源详情
资源评论
收起资源包目录
Cyber Hunter 量子特攻2d横版像素射击游戏项目源码C# (2000个子文件)
cube movement.anim 488KB
idle.anim 51KB
SpaceShipIdle.anim 9KB
DroneHit.anim 7KB
cube B motion.anim 6KB
cube A motion.anim 6KB
VehiclePlatformIdle.anim 6KB
TruckPlatformIdle.anim 5KB
TurretHit.anim 4KB
TurretShooting.anim 4KB
DroneIdle.anim 4KB
PlayerMoving.anim 3KB
PlayerShootingMove.anim 3KB
TurretIdle.anim 3KB
AlienBugIdle.anim 3KB
RotatingGlobe.anim 3KB
dynamicObstacle.anim 3KB
PlayerJumping.anim 3KB
YellowGlobeIdle.anim 3KB
GreenGlobeIdle.anim 3KB
BlueGlobeIdle.anim 3KB
RedGlobeIdle.anim 3KB
DroneDeath.anim 2KB
AlienBugDeath.anim 2KB
SpaceShipDeath.anim 2KB
PlayerDeath.anim 2KB
TurretDeath.anim 2KB
PlayerIdle.anim 2KB
BulletHit.anim 2KB
PlayerShootingIdle.anim 2KB
Bullet.anim 2KB
CyberHunter.apk 56.53MB
AstarPathfindingProjectEditor.asmdef 281B
PackageToolsEditor.asmdef 240B
AstarPathfindingProject.asmdef 184B
DefaultWsdlHelpGenerator.aspx 59KB
DefaultWsdlHelpGenerator.aspx 59KB
DefaultWsdlHelpGenerator.aspx 59KB
Pathfinding_Terrain.asset 881KB
ProjectSettings.asset 24KB
QualitySettings.asset 6KB
InputManager.asset 6KB
GraphicsSettings.asset 2KB
Y Bump Signal Definition.asset 1KB
BlenderSettings.asset 1KB
Physics2DSettings.asset 1KB
NavMeshAreas.asset 1KB
EditorSettings.asset 1KB
DynamicsManager.asset 1KB
UnityConnectSettings.asset 914B
Noise_CM_4.asset 818B
CM ClearShot1 Blends.asset 601B
CM StateDrivenCameraBlends.asset 598B
TagManager.asset 575B
EditorBuildSettings.asset 440B
AudioManager.asset 357B
VFXManager.asset 308B
TimeManager.asset 202B
XRSettings.asset 158B
NetworkManager.asset 151B
PresetManager.asset 146B
ClusterInputManager.asset 114B
sharedassets0.assets 219KB
globalgamemanagers.assets 150KB
Compat.browser 2KB
Compat.browser 2KB
Compat.browser 2KB
Assembly-CSharp.csprojAssemblyReference.cache 744KB
AstarPathfindingProject.csprojAssemblyReference.cache 367KB
AstarPathfindingProjectEditor.csprojAssemblyReference.cache 340KB
PackageToolsEditor.csprojAssemblyReference.cache 338KB
Assembly-CSharp-firstpass.csprojAssemblyReference.cache 171KB
DesignTimeResolveAssemblyReferencesInput.cache 31KB
Assembly-CSharp-Editor.csprojAssemblyReference.cache 9KB
Assembly-CSharp-Editor-firstpass.csprojAssemblyReference.cache 424B
Navmesh.cginc 3KB
machine.config 33KB
machine.config 33KB
machine.config 28KB
web.config 18KB
web.config 18KB
web.config 11KB
config 3KB
boot.config 62B
PlayerController.controller 16KB
Turret.controller 6KB
Drone.controller 6KB
mine_bot.controller 6KB
CharacterController.controller 5KB
Ship.controller 4KB
Shape.controller 4KB
bullet.controller 3KB
YellowGlobe.controller 2KB
v-police.controller 2KB
BlueGlobe.controller 2KB
GreenGlobe.controller 2KB
RedGlobe.controller 2KB
Globe Icon.controller 2KB
v-truck.controller 2KB
Cube.controller 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
小云同志你好
- 粉丝: 1065
- 资源: 1061
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C语言-leetcode题解之83-remove-duplicates-from-sorted-list.c
- C语言-leetcode题解之79-word-search.c
- C语言-leetcode题解之78-subsets.c
- C语言-leetcode题解之75-sort-colors.c
- C语言-leetcode题解之74-search-a-2d-matrix.c
- C语言-leetcode题解之73-set-matrix-zeroes.c
- 树莓派物联网智能家居基础教程
- YOLOv5深度学习目标检测基础教程
- (源码)基于Arduino和Nextion的HMI人机界面系统.zip
- (源码)基于 JavaFX 和 MySQL 的影院管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功