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>();
}
//...
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
2.3MAGIC如何绑定机器人
共2000个文件
meta:5813个
cs:3232个
png:1769个
需积分: 5 0 下载量 154 浏览量
2022-03-15
11:41:54
上传
评论
收藏 218.27MB ZIP 举报
温馨提示
2.3MAGIC如何绑定机器人
资源详情
资源评论
资源推荐
收起资源包目录
2.3MAGIC如何绑定机器人 (2000个子文件)
000771635a267e72a29a00000acbbf5c 28KB
001ecdf21f3659690d0b173a16fb3aa3 5KB
0020ace0a30998b5eac1a6f5e5b49e58 8KB
00342a1f96d4b2d1af4890865196315e 11KB
00364702f6917e02ec4dee9056371ad8 10KB
004779a5e5d4b92873e9dadcc338dadc 8KB
004d04e9ca86f273965b8bf7613e9ea9 402KB
005a83b308abcac14a36f25acac9caec 29KB
0075193326b467cb7c667f83d24d95f5 6KB
007a3bf7910e76e1e220fbcb4099c8f3 22KB
008aa1ccf2e6d32c0afc90af59be1dd2 5KB
009765832c88453bb6993bb8e7bc8604 18KB
00a3fb15f588b7cb0b6eae2df697c888 16KB
00abcab89e0e23cd2b15b1b670c5fdef 6KB
00ba620d48bc4b8e729c8c3192ed8627 349KB
00c5b063b7e770bfc213bf40dc8e6224 29KB
00c61adc43c475cfbf6f6ba08e4ae175 5KB
00ca02f8d01455dbc5b5d59031c42db8 17KB
00d452feb5efe6605607693d662522b5 5KB
00d5974b5138b9f6567d126ce3bca64f 6KB
00e1d866828f41c19cab319ce64a405d 18KB
00e4d91b4edb06f4779f920ec758e587 21KB
0102d19c004a82bc103d7cb00ce22b52 5KB
010af0b4f994eb61c587323d95ff3148 21KB
0110951057b490a6b7b8679c83206077 99KB
0113a0b571ea077ef328159d8c45363f 5KB
0113f1f3987fafd0eee0e7d527e8208e 33KB
011a2d9229681fcaa1dd5c7c66b12409 43KB
012545d07cefa60413fd43eefa1dab0d 5KB
01263b849476c6743af00b680c9fb27c 99KB
0128182b2be4365412d3adc1932172e0 6KB
012910217f57c9d1674d3ed5c2634f8b 17KB
0132c29f00ad0ed9a44a8376cf300596 5KB
013f622af05e2e29f886500ac3675602 18KB
01646ae095268cf3296688c1aa37c00a 18KB
01767a69cad71e7c2db952c93e4206a7 4KB
017780ecbd44a7efcc7b63cf8b5e6b97 159KB
018c65c642d0b1920f93a6c7b6138f4d 5KB
01aa56578618a94dd4c58291cda18ba5 110KB
01cafd33f1a40837974c922ba6ad3d9d 5KB
01cef779d1353e8ba7f7a3b7bdd1eb7a 13KB
01e16201366039d64bf92c6304a56376 42KB
01ea445793b9147fc681328010ea4a5c 19KB
01f86fc02b502fbe05202fba08e9f253 4KB
01f93e79e3093bf5a190344706d5d9db 5KB
02232733264af9db62b64959fb234851 32KB
02398fbf07b8ab0ae8c10d7742b1bd88 76KB
02418f280d7ccfd552eeb9a7b649cc68 25KB
02436a998fcb80d81c8c3842384a3faa 7KB
024960e3ce09249457724e94a55e1ee7 6KB
026ed03287971471398acf8545283593 34KB
027a30d21d2f6fab73f3752be4193d9d 23KB
02aabf2f3e942ae3b3426dd3b616f32f 17KB
02c003de7507c8a6e07afa0babc3fba2 5KB
02c0e17964ea38931c9819cf3fe67285 43KB
02c7fcf7f31665c35989a580395b8102 8KB
02d0ec9b9627a3536263df8287f4ca75 13KB
02d61b5029c1996fc5ba99372b0d229f 14KB
02e3a9305a6c718b83bda20e03930226 5KB
02f186af37e9c68f336f83089345238c 18KB
02f2494a18d8c047ec5a8f90a1dc00d9 5KB
0307565ac7bd698f9f8e11f5b1ca7287 28KB
03113b1e7dd6a5b63fe6a88b165ba886 27KB
032038335b8068f794648a6a953878cc 28KB
032952476c7c9f753e26e63c8db4cc2c 21KB
0341f041ea739d3c05343caa1316f492 4KB
035818814dc5c071dad79d0915d576bb 83KB
037318e22ed2e545d7619e7bdb1de51d 18KB
037ee292309e3c5e1dda6c600185cfe1 9KB
038236eda46cf0d12ea851612c25eb3a 20KB
0391917e571d16edf70d14b6368d722b 30KB
039c5995ac6d717c6ad4c2efe375e29e 20KB
03a052cd72cbaa3c2d58f22459f117a3 6KB
03a4039920263cbced0d808552e52be9 5KB
03b0303cdd97ed3ce3dc709402c025f7 285KB
03b4b788e675f1c12b7e68850f7fb7b0 4KB
03bdfd82c0b9cbcb742115ed706b1800 16KB
03cfa8d84623d775c66991e2bbaeae36 6KB
03d166a3b5ea4fc3e729d2be864c44bc 7KB
03d45b6dc9a51bf8c64480eaeeccc921 4KB
03d50a5109c57bda2ceaaece4cf5401e 28KB
03e4d8ab46f1f927802ca8668c05a710 9KB
03f90fbc4ac6486e4edbccd5094171cf 11KB
0402b40f4549f750b90d7df60f687472 28KB
042224f81c4af0d80bd58edca0998a77 10KB
042c60e946d3d83fd568b5550b3b0859 5KB
0432ea6cf0f4536ec51a16f7d243b667 7KB
043894be15f27e5ecc0f02cf72928787 12KB
043b6945260fc531a7d738362afba733 4KB
044c4cf5e07d52036a27fd85b2c155d6 11KB
044e7b933f24b196141f0092d0b42d46 6KB
0464e9154f119daf434979abe0c942cd 111KB
04682fdb10cbde6ab2b18e5b26ba1f8f 16KB
047761b78bca949d40a4c094da1b7b03 285KB
0492312cb576544d92a1e6f9bae378f0 6KB
04a76340003fb4d7d7c04cb5dffaa93b 25KB
04afdfd9c00ddbeb268d6d42dd574a42 90KB
04b024ea19ebf72368525fcb2fc7ca0c 5KB
04bdf7eefc4e89663404a601981671b6 15KB
04be47758786a4efe6b38d06eb26bee6 6KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
FOHEART
- 粉丝: 54
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0