Home > database >  Unity Player character is not moving
Unity Player character is not moving

Time:11-24

I have a script to move my character(Player) in unity. The script is fine and it does not have any errors, Although when i enter play mode and try to use the arrows to move my character, it does not move at all, i can't figure out what is the problem.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{ 
   public float moveSpeed = 1f;
   public float CollisionOffset = 0.05f;
   public ContactFilter2D movementFilter; 

    Vector2 movementInput;
    Rigidbody2D rb;
    List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();

    // Start is called before the first frame update
    void Start()
    {
      rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate() {
        if (movementInput != Vector2.zero) {
           int count = rb.Cast(
            movementInput,
            movementFilter,
            castCollisions,
            moveSpeed * Time.fixedDeltaTime   CollisionOffset
           );

           if (count == 0) {
            rb.MovePosition(rb.position   movementInput * moveSpeed * Time.fixedDeltaTime);
           }
        }
    }

    void onMove(InputValue movementValue) {
       movementInput = movementValue.Get<Vector2>();
    }
}

Unity version: 2022.2.0b14

Input System: version 1.2.0

Any help is appreciated.

CodePudding user response:

OnMove not executing because it's OnMove not onMove. this is reference directly by new input system. So the Function name should be On [ActionName] and here when you are using Default InputActions so Move -> OnMove.

  • Related