0
votes

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

This is my action map.

enter image description here

1
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
@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
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
@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
@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

1 Answers

0
votes

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