- Home /
Problem setting up twin stick shooter for mobile device...
I have been trying to set up a scene where a cube moves when the left joystick is moved, and rotates when the right is moved. Basically I am trying to make a basic twin stick shooter.
Using some code provided here: http://answers.unity3d.com/questions/28790/twin-stick-shooter.html
var moveSensitivity : float = 3.0;
function Update() {
var lh : float = Input.GetAxis("LeftStickHorizontal");
var lv : float = Input.GetAxis("LeftStickVertical");
var rh : float = Input.GetAxis("RightStickHorizontal");
var rv : float = Input.GetAxis("RightStickVertical");
//Assumes you're looking down the z axis
transform.position += Vector3(lh, lv, 0.0).normalized * moveSensitivity
* Time.deltaTime;
//Assumes you're looking down the z axis and that you are looking down on the avatar
transform.LookAt(transform.position + Vector3(rh, rv, 0.0), -Vector3.forward);
}
I set up my inputs up accordingly and made them x or y-axis only and the types to joystick. But for some reason it still does not work. I am not getting any errors from the editor.
I think the reason my cube isn't moving is because my rh and rl (etc) values are fixed at zero. How come they don't change from between 1 to -1 (when I move my joysticks) like they are supposed to.
All help is appreciated. Thanks
Answer by WillTAtl · Nov 27, 2011 at 07:00 PM
Possible I'm wrong here, but unless I'm mistaken you can't read on-screen thumbsticks through unity's usual Input.GetAxis() methods. The original question was using a physical game controller, specifically a 360 one; these are supported by the Input class, but at present virtual on-screen controls for touch-screen devices are not.
Assuming you're using the on-screen sticks from the standard mobile assets, you have to read their value by accessing the script for them and reading the horizontal and vertical values there. In the standard mobile assets there's also a script FirstPersonControl that uses the DualJoysticks prefab.
You'll want variables to reference the joystick objects, like so...
var leftStick: Joystick;
var rightStick: Joystick;
These can be assigned in the inspector by dragging the left and right stick objects to them. Then you access the stick's positions through the leftStick.position and rightStick.position, which are simply vectors. Specifically, "leftStick.position.x" would be the equivalent of "Input.GetAxis("LeftStickHorizontal")".
The example script is fairly well-commented, and should help you with any other problems you run into (assuming you don't find it good enough for your needs as-is!)
Hope this helps!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
AddTorque doesn't rotate? 3 Answers
Mecanim character wobbles when changing camera relative direction. 0 Answers
Material doesn't have a color property '_Color' 4 Answers
how to integrate steering wheel controller for ps3 in unity?? 0 Answers