I'm making a 2D game with Unity using the new Input System. I want to flip the player character when he changes directions.
I tried using spriteRenderer.flipX
to flip the player, but since he is made of three parts (body and two eyes), it didn't work (I couldn't convert them to a single sprite because I needed the eyes 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 this error means and how I can fix it.
Now, the player can jump but he can't move or flip. You can see the code, the error, and my action map 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?1D 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.)Left Stick[Gamepad]
binding. If I useLeft
andRight
instead of that, the player won't move.