最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Unity input system function OnMove is unused - Stack Overflow

programmeradmin1浏览0评论

hey i've followed the unity roll ball tutorial but my OnMove function seems to be unused then my player won't move in unity

this is my code:

using UnityEngine;
using UnityEngine.InputSystem;

public class playerController : MonoBehaviour
{
    private Rigidbody rb;
    private float moveX;
    private float moveY;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>(); //ref to player's rigidbody
    }

    void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        moveX = movementVector.x;
        moveY = movementVector.y;
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(moveX, 0.0f, moveY);
        rb.AddForce(movement);
    }
}

unity windows

hey i've followed the unity roll ball tutorial but my OnMove function seems to be unused then my player won't move in unity

this is my code:

using UnityEngine;
using UnityEngine.InputSystem;

public class playerController : MonoBehaviour
{
    private Rigidbody rb;
    private float moveX;
    private float moveY;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>(); //ref to player's rigidbody
    }

    void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        moveX = movementVector.x;
        moveY = movementVector.y;
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(moveX, 0.0f, moveY);
        rb.AddForce(movement);
    }
}

unity windows

Share Improve this question edited Feb 6 at 6:19 Ando Razafy asked Feb 6 at 4:24 Ando RazafyAndo Razafy 112 bronze badges 11
  • You may want to cleanup your spelling to make your question clearer. I'm pretty sure "inpu" is a typo for "input", that "tuto" is somehow short "tutorial" (which autocorrect suggested as I typed "tuto" just now). What's "unsed"? – Flydog57 Commented Feb 6 at 4:32
  • OnMove function does not seem to be unused. It is unused. It cannot be used. This function is private and not called anywhere. This way, is cannot be used and makes no sense. Is that you wanted to understand? You did not ask a question. – Sergey A Kryukov Commented Feb 6 at 4:39
  • 1 @SergeyAKryukov not completely true actually! To me it looks like this is supposed to be used alongside the PlayerInput component which can be set to use SendMessage and invoke the method via Unity's messaging system which can use private methods – derHugo Commented Feb 6 at 5:40
  • Can you show us your Inspector for that object? Do you have the PlayerInput component? Can you also show us your InputActions? – derHugo Commented Feb 6 at 5:40
  • 1 @SergeyAKryukov it's not reflection.. it works somewhat similar but way more efficient. It's a messaging system - you will note that all of OPs methods are private. Unity event messages like Start, FixedUpdate and many more are discovered on compile time and hooked directly into the underlying c++ engine player loop callbacks. It's not really something we chose .. it's how Unity does it – derHugo Commented Feb 6 at 7:58
 |  Show 6 more comments

1 Answer 1

Reset to default 0

Start by assigning your input action asset in the player input component.

At the bottom (above the buttons Open Input Settings and Open Input Debugger) there is a list of the function names that will be called.

In my case I have actions Mouse1 and Mouse4, but in the list they are called OnMouse1 and OnMouse4.

private void OnMouse1(UnityEngine.InputSystem.InputValue obj)
{
    Debug.Log($"Mouse 1");
}
private void OnMouse4(UnityEngine.InputSystem.InputValue obj)
{
    Debug.Log($"Mouse 4");
}

Rename your functions in the code if needed, and it should work!

发布评论

评论列表(0)

  1. 暂无评论