Код: Выделить всё
using UnityEngine;
using UnityEngine.InputSystem;
public class Player: MonoBehaviour
{
public class Axis
{
public bool up = false;
public bool down = false;
public bool left = false;
public bool right = false;
}
public Axis fixation = new Axis();
public Rigidbody2D rb;
public PlayerInput playerInput;
private void Start()
{
rb = GetComponent();
playerInput = GetComponent();
PlayerInputAction input = new PlayerInputAction();
input.Enable();
input.Gameplay.FreezeUp.performed += (InputAction.CallbackContext _) => FixateAxes("up", true);
input.Gameplay.FreezeUp.canceled += (InputAction.CallbackContext _) => FixateAxes("up", false);
input.Gameplay.FreezeDown.performed += (InputAction.CallbackContext _) => FixateAxes("down", true);
input.Gameplay.FreezeDown.canceled += (InputAction.CallbackContext _) => FixateAxes("down", false);
input.Gameplay.FreezeLeft.performed += (InputAction.CallbackContext _) => FixateAxes("left", true);
input.Gameplay.FreezeLeft.canceled += (InputAction.CallbackContext _) => FixateAxes("left", false);
input.Gameplay.FreezeRight.performed += (InputAction.CallbackContext _) => FixateAxes("right", true);
input.Gameplay.FreezeRight.canceled += (InputAction.CallbackContext _) => FixateAxes("right", false);
}
private void FixedUpdate()
{
Vector2 v = rb.linearVelocity;
if (fixation.up && v.y > 0) v.y = 0;
if (fixation.down && v.y < 0) v.y = 0;
if (fixation.left && v.x < 0) v.x = 0;
if (fixation.right && v.x > 0) v.x = 0;
rb.linearVelocity = v;
}
private void FixateAxes(string axis, bool stat)
{
switch (axis)
{
case "up":
fixation.up = stat;
break;
case "down":
fixation.down = stat;
break;
case "left":
fixation.left = stat;
break;
case "right":
fixation.right = stat;
break;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... o-one-side
Мобильная версия