2

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. enter image description here

This is my action map.

enter image description here

5
  • This error means it cannot cast AxisComposite to Vector2, because you're using Pass Through action type which reads and stores (inside AxisComposite) all the inputs at once, regardless of which direction is "the strongest". What happens when you change the type to Value?
    – BFyre
    Commented Dec 1, 2020 at 1:23
  • @BFyre Nothing special happens when I change the type to Value. Just the errors disappear and it seems like the problem is solved but when I play the game, the player still can't move and I get the same error in the console.
    – Ali
    Commented Dec 1, 2020 at 1:51
  • And what if you use directions instead of negative/positive? I cannot check it right now, but look how it was done in an answer here: stackoverflow.com/questions/58469484/…
    – BFyre
    Commented Dec 1, 2020 at 9:29
  • @BFyre I changed gamepad movement from 1D Axis to Binding (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.)
    – Ali
    Commented Dec 1, 2020 at 23:01
  • @BFyre I tried changing the keyboard input to binding but it didn't work. Also, I found something weird. After I changed the gamepad input to binding, The player can go up and down with the left stick and that's because of Left Stick[Gamepad] binding. If I use Left and Right instead of that, the player won't move.
    – Ali
    Commented Dec 2, 2020 at 22:19

1 Answer 1

1

I changed my action map to this and now the flipping is working and I don't get that error anymore.

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.