- Home /
Virtual joystick for roll a ball game
Hey guys, I have to say that I am quite new to Unity. I did the official Tutorial for the Roll a Ball game and it works just fine with my keyboard. Now I want to create a joystick so that I can play the game on my Android mobile. I used the new Input System and tried to implement a joystick with it. It kinda works, but the ball moves very weird.. sometimes I can change the direction of the ball and sometimes I cannot. I added OnScreen Stick to my Joystick Image and added the left Stick. I would really appreciate it if somebody could help me, I am stuck for a long time already. My Script for my Player looks like this:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour { protected Joystick Joystick; public float speed = 0; private Rigidbody rb; private float movementX; private float movementY; void Start() { rb = GetComponent();
}
void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
}