Как исправить ошибку управления (Input system)?
Пытаюсь подключить скрипт контроллера игрока из чужого проекта (рабочий проект - человек сделал игру, а я скачал его файлы скриптов). Установил и настроил Input system. Создал Input Actions с именем Controls, получил одноименный Controls.cs. Запускаю игру - нажимаю пробел игрок прыгает, в консоли ошибок нет.
Нажимаю w - игрок не прыгает, в консоли ошибка. Нажимаю a или d - игрок не перемещается, в консоли ошибка.
Скорее всего нужно что то изменить в Input system, потому что скрипты ниже работают у их автора. К сожалению, как была настроена система ввода неизвестно.
Как исправить ошибку управления (Input system)?
Текст ошибки при нажатии w
! InvalidOperationException: Cannot read value of type 'Single' from composite 'UnityEngine.InputSystem.Composites.Vector2Composite' bound to action 'Player/Move[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d,/Keyboard/upArrow,/Keyboard/downArrow,/Keyboard/leftArrow,/Keyboard/rightArrow]' (composite is a 'Int32' with value type 'Vector2') UnityEngine.InputSystem.InputActionState.ReadValue[TValue] (System.Int32 bindingIndex, System.Int32 controlIndex, System.Boolean ignoreComposites) (at Library/PackageCache/[email protected]/InputSystem/Actions/InputActionState.cs:2808) UnityEngine.InputSystem.InputAction+CallbackContext.ReadValue[TValue] () (at Library/PackageCache/[email protected]/InputSystem/Actions/InputAction.cs:1939) GatherInput.StartMove (UnityEngine.InputSystem.InputAction+CallbackContext ctx) (at Assets/Scenes/physics test scene/GatherInput.cs:50) UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue] (UnityEngine.InputSystem.Utilities.CallbackArray
1[System.Action1[TValue]]& callbacks, TValue argument, System.String callbackName, System.Object context) (at Library/PackageCache/[email protected]/InputSystem/Utilities/DelegateHelpers.cs:46) UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
Код скриптов
PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpForce = 0.0f;
private Rigidbody2D rb;
private GatherInput gI;
public float rayLength;
public bool grounded;
public bool preJump = false;
public LayerMask groundLayer;
public Transform checkPointLeft;
public Transform checkPointRight;
public PhysicsMaterial2D bounceMat, normalMat;
private int direction = 1;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
gI = GetComponent<GatherInput>();
}
private void FixedUpdate()
{
Flip();
PlayerJump();
CheckStatus();
PlayerMove();
}
private void PlayerMove()
{
if (jumpForce == 0.0f && grounded)
{
rb.velocity = new Vector2(speed * gI.valueX, rb.velocity.y);
}
}
private void Flip()
{
if(gI.valueX * direction < 0)
{
transform.localScale = new Vector3(-transform.localScale.x, 1, 1);
direction *= -1;
}
}
private void PlayerJump()
{
if(gI.jumpInput && grounded)
{
jumpForce += 0.5f;
rb.velocity = new Vector2(0.0f, rb.velocity.y);
rb.sharedMaterial = bounceMat;
preJump = true;
}
else
{
preJump = false;
}
if (gI.jumpInput && grounded && jumpForce >= 20.0f || gI.jumpInput == false && jumpForce >= 0.1f)
{
float tempX = gI.valueX * speed;
float tempY = jumpForce;
rb.velocity = new Vector2(tempX,tempY);
Invoke("ResetJump", 0.025f);
}
if (rb.velocity.y <= -1)
{
rb.sharedMaterial = normalMat;
}
}
private void ResetJump()
{
jumpForce = 0.0f;
}
private void CheckStatus()
{
RaycastHit2D leftCheckHit = Physics2D.Raycast(checkPointLeft.position, Vector2.down, rayLength, groundLayer);
RaycastHit2D rightCheckHit = Physics2D.Raycast(checkPointRight.position, Vector2.down, rayLength, groundLayer);
if (leftCheckHit || rightCheckHit)
{
grounded = true;
}
else
{
grounded = false;
}
SeeRays(leftCheckHit, rightCheckHit);
}
private void SeeRays(RaycastHit2D leftCheckHit, RaycastHit2D rightCheckHit)
{
Color color1 = leftCheckHit ? Color.green: Color.red;
Color color2 = rightCheckHit ? Color.green: Color.red;
Debug.DrawRay(checkPointLeft.position, Vector2.down * rayLength, color1);
Debug.DrawRay(checkPointRight.position, Vector2.down * rayLength, color2);
}
}
GatherInput.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class GatherInput : MonoBehaviour
{
public Controls myControls;
//X axis input direction
public float valueX;
//Identify Jump Input
public bool jumpInput;
private void Awake()
{
myControls = new Controls();
}
private void OnEnable()
{
myControls.Player.Move.performed += StartMove;
myControls.Player.Move.canceled += StopMove;
myControls.Player.Jump.performed += JumpStart;
myControls.Player.Jump.canceled += JumpStop;
myControls.Player.Enable();
}
private void OnDisable()
{
myControls.Player.Move.performed -= StartMove;
myControls.Player.Move.canceled -= StopMove;
myControls.Player.Jump.performed -= JumpStart;
myControls.Player.Jump.canceled -= JumpStop;
myControls.Player.Disable();
}
private void StartMove(InputAction.CallbackContext ctx)
{
valueX = ctx.ReadValue<float>();
}
private void StopMove(InputAction.CallbackContext ctx)
{
valueX = 0;
}
private void JumpStart(InputAction.CallbackContext ctx)
{
jumpInput = true;
}
private void JumpStop(InputAction.CallbackContext ctx)
{
jumpInput = false;
}
private void Update()
{
if (Keyboard.current.escapeKey.isPressed){
Application.Quit();
}
}
}
