0
\$\begingroup\$

I've been following Brackey's tutorial on this, see the excerpt from my code below. In his tutorial it only teaches "transform.Translate", how do I use this gamepad input system tool to add forces to the character to make them move instead? So I can have jump physics, and when they're in space they'll keep the momentum.

{
    NewControls controls;

    Vector2 move;
    Vector2 rotate;
    private void Awake()
    {
        controls = new NewControls();

        controls.Gameplay.Jump.performed += ctx => Jump();

        controls.Gameplay.Walk.performed += ctx => move = ctx.ReadValue<Vector2>();
        controls.Gameplay.Walk.canceled += ctx => move = Vector2.zero;

        controls.Gameplay.Rotate.performed += ctx => rotate = ctx.ReadValue<Vector2>();
        controls.Gameplay.Rotate.canceled += ctx => rotate = Vector2.zero;
    }

    private void Update()
    {
        Vector2 m = new Vector2(move.y, move.x) * Time.deltaTime;
        transform.Translate(m, Space.Self);**

        Vector2 r = new Vector2(rotate.y, rotate.x) * 100f * Time.deltaTime;
        transform.Rotate(r, Space.Self);
    }
}

To move with rigidbody controls, you have to use things like “forward” or “left” but I want one joystick to control the forward, backwards, and side to side movements. I don’t know how to use the joystick input for moving in all those directions. The tutorials have them as separate arrow keys

does anyone have some sample code, or a tutorial to point me to, or just some explanation? it would be much appreciated.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Presumably you started by typing "control character with physics in Unity" into a search engine, and found the wealth of tutorials showing how to do this with a Rigidbody or Rigidbody2D component? What have you tried based on your research so far? Where did you run into a specific obstacle that we can help you overcome? \$\endgroup\$ – DMGregory Feb 20 at 21:20
  • \$\begingroup\$ The other tutorials are for keyboard controls, and not using the controller package like in this brackeys video youtu.be/p-3S73MaDP8 I don’t know how to have the rigidbody movement without changing the input system \$\endgroup\$ – anura Feb 20 at 21:28
  • \$\begingroup\$ The input source does not change the Rigidbody methods. These are orthogonal topics. Try editing your question to show how you've tried replacing your Transform code with Rigidbody code, and explain in detail where you're having trouble. \$\endgroup\$ – DMGregory Feb 20 at 21:39
  • \$\begingroup\$ To move with rigidbody controls, you have to use things like “forward” or “left” but I want one joystick to control the forward, backwards, and side to side movements. I don’t know how to use the joystick input for moving in all those directions. The tutorials have them as separate arrow keys \$\endgroup\$ – anura Feb 20 at 22:48
  • \$\begingroup\$ Your code already shows how to combine a horizontal and vertical movement into a vector that you can use as a force or a velocity. There is no requirement to use only "forward" or "left" when using Rigidbodies. \$\endgroup\$ – DMGregory Feb 20 at 22:56
0
\$\begingroup\$

I'm pretty sure I've solved my problem! I used

rb.velocity = new Vector3(move.x, move.y);

instead of

rb.AddForce(move.x, move.y);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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