Joystick Shake

How can I get such a script that when it hit the wall so the joystick shakes.

This could help

to adapt to your needs.

I took that part of the script. How do I get this done so that when it hit the wall so the joystick shakes.

using UnityEngine;
using XInputDotNetPure; // Required in C#

public class NewBehaviourScriptt : MonoBehaviour
{
    bool playerIndexSet = false;
    PlayerIndex playerIndex;
    GamePadState state;
    GamePadState prevState;

    // Use this for initialization
    void Start()
    {
        // No need to initialize anything for the plugin
    }

    void FixedUpdate()
    {
        // SetVibration should be sent in a slower rate.
        // Set vibration according to triggers
        GamePad.SetVibration(playerIndex, state.Triggers.Left, state.Triggers.Right);
    }

    // Update is called once per frame
    void Update()
    {
        // Find a PlayerIndex, for a single player game
        // Will find the first controller that is connected ans use it
        if (!playerIndexSet || !prevState.IsConnected)
        {
            for (int i = 0; i < 4; ++i)
            {
                PlayerIndex testPlayerIndex = (PlayerIndex)i;
                GamePadState testState = GamePad.GetState(testPlayerIndex);
                if (testState.IsConnected)
                {
                    Debug.Log(string.Format("GamePad found {0}", testPlayerIndex));
                    playerIndex = testPlayerIndex;
                    playerIndexSet = true;
                }
            }
        }

        prevState = state;
        state = GamePad.GetState(playerIndex);

    }
}

ok lets say when you hit the wall (using OnCollisionEnter(Collision collision)) for example or whatever you set a bool named isInCollision to true.

if(isInCollision)
{
GamePad.SetVibration(playerIndex, 1, 1);

}

playerIndex is already sat (your Xbox pad if monoplayer)
1 and 1 are left and right motors intensities.
and when your are not in collision (isInCollision = false)

GamePad.SetVibration(playerIndex, 0, 0);

ps: you don’t need this

 GamePad.SetVibration(playerIndex, state.Triggers.Left, state.Triggers.Right);

Thanks!

you are welcome.
Note that you can manage all pad buttons without any input settings in unity. :wink:

I’m really bad coding, so if you can write me the code completely. :slight_smile:

Just study the code. all you need is in.
i give you some examples

if (Input.GetButton("Button name in the input settings"))

become

if (prevState.Buttons.X == ButtonState.Pressed) // true if you press X button
MoveH = Input.GetAxis("Horizontal"); // the value of X axis of lef joystick

become

MoveH = state.ThumbSticks.Left.X;

etc…
enjoy