0
\$\begingroup\$

I was asking myself if you could change the position of a game object with the input of the player, with the new input system.

That is, with something like this:

OnMove() {

//Code to change game object's position

}

I want to make a script where the position of the game object is changed to a specific position, based on the joystick value and the WASD keys. That is, there are multiple positions the game object can go to.

This is for a top-down game where a gameobject should go to differnt places around the player. Since this gameobject is parented to the player and the gameobject is always touching the player, positions from -1 to 1 are enough.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ The title of this question is almost identical to your previous question, but contains much less detail. Also, that question seems to include a method to move an object using the new input system, so it seems you already know the answer to this question? Can you clarify why these are two separate posts, and what you need help with? In what way does the method you showed in your previous question not serve the needs you have in this question? \$\endgroup\$
    – DMGregory
    Commented Jan 17, 2023 at 3:48
  • 1
    \$\begingroup\$ You may want to add detail about what you mean with "multiple positions" — reading between the lines, I think what you're asking is to classify the input into one of the 8 cardinal or semi-cardinal directions, and place your sword / attack range object at a point on a circle relative to its parent so that it points in this direction? This is the same topic as your previous question, so it should have been an edit, not a new question. \$\endgroup\$
    – DMGregory
    Commented Jan 17, 2023 at 12:12

1 Answer 1

1
\$\begingroup\$

I assume you already have an Input Action asset with an action "Move" of control type "Vector2" that has bindings for both the joystick and the WASD keys and that you added a PlayerInput component to your scene.

If your PlayerInput component is configured to "Send Messages", then your method needs to be on the same gameobject as the PlayerInput component and have a method which looks like this:

public void OnMove(InputValue value)
{
    Vector2 input = value.Get<Vector2>();
}

If your PlayerInput component is configured to use UnityEvents, then the method you bind to the Move action via inspector needs to look like this:

public void OnMove(InputAction.CallbackContext context)
{
     Vector2 input = context.Get<Vector2>();
}

The vector you receive has x and y components between -1 and +1. For keyboard keys and other digital input buttons, those will always be 1.0f, 0f or -1.0f. But joysticks are analog input devices which will deliver floating-point values anywhere in this range. So if you want to support both digital and analog input devices, you need to check for ranges, not for equality.

Now all you need to do is map the possible values of the input vector to specific positions and then set the transform.position to that position. I could go into further detail, but your question is too vague to tell what your actual requirements are in this regard.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks Philipp this really helped me, but I'm not a very expirienced developer yet, so how can you "map the possible values of the input vector to specific positions." I have literally no idea how to do that. Would you do that with if, else statements or what. And do you have to do that seperatly for keyboard and gamepad?(Im using them both). \$\endgroup\$
    – Hammynator
    Commented Jan 17, 2023 at 11:36
  • \$\begingroup\$ @Hammynator Yes, using an if/else tree would be an option. A more experienced developer might use vector math or a lookup table, though. No, you don't need separate code for keyboard and gamepad, because one goal of the new input system is to abstract input devices into semantic actions. Just keep in mind what I wrote regarding digital vs. analog inputs. \$\endgroup\$
    – Philipp
    Commented Jan 17, 2023 at 11:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .