I'm making a 2D game with Unity using the new Input System. I used spriteRenderer.flipX
to flip the player but since it is made of three parts (body and two eyes), the spriteRenderer
didn't work. Also, I couldn't convert them to a single sprite because I need the eyes in order to animate them. So, I decided to use transform.localscale
for flipping and change movementInput
value from a float to a Vector2. The problem is that when I press the arrow or AD keys to move the character, an error pops up saying "Cannot read value of type Vector2 from composite". This error is from InputActionState
which is a long and complicated code related to the new Input System. Also, getting input from gamepad causes the same error. I don't know what does this error mean and how can I fix it. Now, the player can jump but it can't move or flip. You can see the code, the error and my action map down below.
This is my Player Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float speed, jumpSpeed;
[SerializeField] private LayerMask ground;
private PlayerActionControls playerActionControls;
private Rigidbody2D rb;
private PolygonCollider2D pol;
private Animator animator;
private bool facingRight = true;
private Vector2 movementInput;
private void Awake() {
playerActionControls = new PlayerActionControls();
rb = GetComponent<Rigidbody2D>();
pol = GetComponent<PolygonCollider2D>();
animator = GetComponent<Animator>();
}
private void OnEnable() {
playerActionControls.Enable();
}
private void OnDisable() {
playerActionControls.Disable();
}
void Start()
{
playerActionControls.Land.Jump.performed += ctx => Jump(ctx.ReadValue<float>());
}
private void Jump(float val) {
if (val == 1 && IsGrounded()) {
rb.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
}
}
private bool IsGrounded() {
Vector2 topLeftPoint = transform.position;
topLeftPoint.x -= pol.bounds.extents.x;
topLeftPoint.y += pol.bounds.extents.y;
Vector2 bottomRightPoint = transform.position;
bottomRightPoint.x += pol.bounds.extents.x;
bottomRightPoint.y -= pol.bounds.extents.y;
return Physics2D.OverlapArea(topLeftPoint, bottomRightPoint, ground);
}
void FixedUpdate()
{
if(facingRight == false && movementInput.x > 0){
Flip();
} else if (facingRight == true && movementInput.x < 0){
Flip();
}
}
void Flip(){
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void Update()
{
Vector2 movementInput = playerActionControls.Land.Move.ReadValue<Vector2>();
Vector2 currentPosition = transform.position; // This was a Vector3 but since I got the error "Operator '+=' is ambiguous on operands of type 'Vector3' and 'Vector2", I changed it to a Vector2.
currentPosition += movementInput * speed * Time.deltaTime;
transform.position = currentPosition;
}
}
This is the error I get when I want to move the player.
This is my action map.
AxisComposite
toVector2
, because you're using Pass Through action type which reads and stores (insideAxisComposite
) all the inputs at once, regardless of which direction is "the strongest". What happens when you change the type to Value? – BFyre1D Axis
toBinding
(like that answer) and I was able to move the player with gamepad! But still I get the same error when I press any button on keyboard. Do I need to change the keyboard input to binding as well? (Also I updated the action map image.) – AliLeft Stick[Gamepad]
binding. If I useLeft
andRight
instead of that, the player won't move. – Ali