Joystick Help

Trying to add a joystick to the Space Shooter tutorial. Do I add joystick script to the Player Controller script? Any help would be greatly appreciated, thank you!

you can use the input class the same way with joysticks as well as keyboard keys.

If you’re interested I’ve made for way back a dynamic system handle purely by code (and it’s free) here: Dropbox - Error - Simplify your life but it may be slightly outdated (I’ve reworked a few bugs) if you’d like I can probably update it. It also works with joystick and is fully customizable.

void Update()
{
if (Input.GetButton (“Fire1”) Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate (bullet, bulletSpawn1.position, bulletSpawn1.rotation);
Instantiate (bullet, bulletSpawn2.position, bulletSpawn2.rotation);
audio.Play ();
}
}

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}

Is the code from Space Shooter tutorial. There has to be a way to add joystick code to this somehow to be able to move the ship and fire. When I play on my Android, all I can do is shoot.

I was able in bring in the joystick code found here http://forum.unity3d.com/threads/29432-Converted-Penelope-s-Javascript-Joysticks-to-C-%28c-sharp%29, and then wrote my own logic in the PlayerController of the Space Shooter tutorial:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;

public GameObject bullet;
public Transform bulletSpawn1;
public Transform bulletSpawn2;
public float fireRate;

private float nextFire;

public Joystick moveJoystick;
public Joystick fireJoystick;

void Update()
{
if (fireJoystick.tapCount == 1 Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate (bullet, bulletSpawn1.position, bulletSpawn1.rotation);
Instantiate (bullet, bulletSpawn2.position, bulletSpawn2.rotation);
audio.Play ();
}
}

void FixedUpdate()
{
Vector3 movement = new Vector3 (moveJoystick.position.x, 0.0f, moveJoystick.position.y);

Vector2 absJoyPos = new Vector2 (Mathf.Abs (moveJoystick.position.x), Mathf.Abs (moveJoystick.position.y));
if (absJoyPos.y > absJoyPos.x)
{
if (moveJoystick.position.y > 0)
{
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
}
else
{
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
}
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}

void OnEndGame()
{
moveJoystick.Disable ();
fireJoystick.Disable ();
this.enabled = false;
}
}

When I push in any direction on the joystick, the ship shoots out of the screen. I’m stumped at the moment. Maybe I’ll slow down on the speed since it’s at 10, for now. Or maybe I should try changing the Vector2 to Vector3?

I was able to get joystick to work with the Ship in Space Shooter tutorial but I don’t think the joystick moves fast enough to move the Ship.

Here is my updated code for moving the Ship:

void FixedUpdate()
{
Vector3 movement = new Vector3 (moveJoystick.position.x, 0.0f, moveJoystick.position.y);

rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}

If anyone has an idea of how I can make the joystick move faster, I’m open to suggestions!!

I am trying to use your code, but I do not quite understand how it works; also it is giving me an error saying that the code needs to be derived from MonoBehavior.