Use Joystick Axis As Buttons?
Hello Unity3D.I have a question about joysticks?How can i make it that i can use the axis of my joystick as a button or buttons?For Example.I use a ps4 controller for my game and i want to use the right analog stick axis as a button like if i push the stick up and it would equal the jump button and if i push the stick down it would equal the block button if i push it to the right it would equal punch button if i push it to the left it would equal the projectile button.If anyone knows how to do this.Can you please tell me how?
Answer by bowloflol · Dec 30, 2015 at 02:00 AM
Go to Edit -> Input Settings and play around with the settings there. To actually use the inputs in a script you'll need to use strings, here's a small example.
var playerMove : float = 5;
function Update()
{
if (Input.GetAxis("Horizontal"))
{
transform.localPosition.x += playerMovement * Input.GetAxis("Horizontal") * Time.deltaTime;
}
if (Input.GetAxis("Depth"))
{
transform.localPosition.z += playerMovement * Input.GetAxis("Horizontal") * Time.deltaTime;
}
}
my bad it should be
var player$$anonymous$$ovement : float = 5;
Wait but suppose if i want to use the analog stick for 4 buttons.Like as in Punch,$$anonymous$$ick,Block,Grab?
You can add, edit, delete and duplicate inputs to your hearts content. And an updated script for that would look something like this
var player$$anonymous$$ovement : float = 5;
var playerAttacks : float = 0;
function Update()
{
if (Input.GetAxis("Horizontal1"))
{
transform.localPosition.x += player$$anonymous$$ovement * Input.GetAxis("Horizontal1") * Time.deltaTime;
}
if (Input.GetAxis("Depth1"))
{
transform.localPosition.z += player$$anonymous$$ovement * Input.GetAxis("Depth1") * Time.deltaTime;
}
if (Input.GetAxis("Horizontal2"))
{
if (Input.GetAxis("Horizontal2") == 1)
{
Jump;
}
if (Input.GetAxis("Horizontal2") == -1)
{
Block;
}
}
if (Input.GetAxis("Depth2"))
{
if (Input.GetAxis("Depth2") == 1)
{
Punch;
}
if (Input.GetAxis("Depth2") == -1)
{
$$anonymous$$ick;
}
}
}
Answer by A_Li_N · Dec 30, 2015 at 05:46 AM
Take my following note with a grain of sand...I could have been mis-understanding how sensitivity works... Read more here: http://docs.unity3d.com/Manual/class-InputManager.html
A quick note that inputs have a sensitivity, meaning even a click of a key on a keyboard is not instanly '1' on the axis, it will build up from 0 to 1 based on the sensitivity.
Changing those '== 1' and '== -1' to '> 0' and '< 0' will make the action instant as soon as the axis is pressed. (or setting the sensitivity of the inputs to 0)
I didn't even think about that, you are absolutely correct, thank you.
Your answer
Follow this Question
Related Questions
Detecting joystick disconnect 2 Answers
Sprite is drawn over canvas element 0 Answers
Controller Stick Input "Snaps" 0 Answers
How can I listen for input from all joystick axes through c sharp code? 0 Answers