- Home /
How to set joystick for movement
Hello, how can I set a joystick from those on the asset store or just normal buttons to replace these actions on a mobile device? I've been struggling to integrate them and I'm quite new to all this as well. Also, if you have any ideas on how to make this code better, then hope you can help me as well. Thanks a lot!
public Rigidbody rb;
public float moveForce = 5f;
void Update()
{
if (Input.GetKey("d") || (Input.GetKey(KeyCode.RightArrow)))
{
rb.AddForce(moveForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a") || (Input.GetKey(KeyCode.LeftArrow)))
{
rb.AddForce(-moveForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("w") || (Input.GetKey(KeyCode.UpArrow)))
{
rb.AddForce(0, 0, moveForce * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey("s") || (Input.GetKey(KeyCode.DownArrow)))
{
rb.AddForce(0, 0, -moveForce * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey("e"))
{
transform.Rotate(Vector3.up * speed * Time.deltaTime);
}
if (Input.GetKey("q"))
{
transform.Rotate(Vector3.down * speed * Time.deltaTime);
}
Answer by Llama_w_2Ls · Aug 20, 2020 at 06:28 PM
First of all, youre using a lot of unnecessary ifs. You can just use` Input.GetAxis("Horizontal")` to get left arrow and right arrow, and same for vertical axis. Nevertheless, using the free joystick pack in the unity asset store, you can access a class containing the types of joysticks available. Im using the variableJoystick class in this case but you can use any of the different types of joysticks. So you can basically write:
public VariableJoystick moveJoystick;
public VariableJoystick lookJoystick;
//Two joystick references, one for movement and one for looking around
public Rigidbody rb;
public float moveForce = 5f;
public float speed = 5f;
private void Update()
{
float x = moveJoystick.Horizontal; //Equals the joystick handle's position from the center of the joystick on the horizontal axis
float z = moveJoystick.Vertical; //Equals the joystick handle's position from the center of the joystick on the vertical axis
float y = lookJoystick.Vertical;
Vector3 moveDirection = transform.right * x + transform.forward * z; //Direction of movement
Vector3 lookDirection = new Vector3(0, y, 0); //Direction of look rotation
rb.AddForce(moveDirection * Time.deltaTime, ForceMode.VelocityChange); //Constantly adds force depending on where the handle of the joystick is
transform.Rotate(lookDirection * speed * Time.deltaTime); //Looks in the right direction depending on the other joystick's y position
}
This uses two VariableJoystick's. One for movement and the other for the look controls. These classes are only available from the free joystick asset pack on the unity asset store. Hope it helps @Amberlewis012
Answer by Amberlewis012 · Aug 21, 2020 at 06:35 AM
Ok so I tried putting the code in the player script, however it doesn't allow me to put the joystick in the move joystick slot thing, and if I put in the joystick from the prefab it allows me to do it but doesn't do anything when I play. What did I do wrong or is it because my player is not in the actual scene or something? Thanks a lot for helping anyways @Llama_w_2Ls (also just realised this isn't a reply but whatever it works)
You need to create a canvas first, and then drag in the VariableJoystick prefab into the canvas in your scene. Then connect that to your moveJoystick variable in the script and it should be working. Also make sure that your player has a rigidbody and that it is assigned to the public rigidbody slot in the script.
Are you getting any distinct errors or warnings? If so, they might be in the script or they'll let you know what you have to do to get this working. @Amberlewis012
no errors or warnings or anything, still doesn't let me do it. Normally when you drag it in the slot in the script you get a little plus icon before you release the mouse, but it just doesn't show up for me.
I think it might be because the player isn't in the hierarchy and that's part of the game mechanic so I don't think I can change it (edit: nope, doesn't work if I put the player in the scene, nor does it work with other joysticks as well), but what do I know, I'm the one asking for help here. @Llama_w_2Ls
Create a canvas and attached the script there
Answer by kms171wb · Jun 03 at 05:33 AM
can you tell me where i can place this public VariableJoystick moveJoystick; public VariableJoystick lookJoystick; //Two joystick references, one for movement and one for looking around
public Rigidbody rb;
public float moveForce = 5f;
public float speed = 5f;
private void Update()
{
float x = moveJoystick.Horizontal; //Equals the joystick handle's position from the center of the joystick on the horizontal axis
float z = moveJoystick.Vertical; //Equals the joystick handle's position from the center of the joystick on the vertical axis
float y = lookJoystick.Vertical;
Vector3 moveDirection = transform.right * x + transform.forward * z; //Direction of movement
Vector3 lookDirection = new Vector3(0, y, 0); //Direction of look rotation
rb.AddForce(moveDirection * Time.deltaTime, ForceMode.VelocityChange); //Constantly adds force depending on where the handle of the joystick is
transform.Rotate(lookDirection * speed * Time.deltaTime); //Looks in the right direction depending on the other joystick's y position
}
player or canvas???????????