# Unity - Touch Input help needed

I'm just starting to learn Unity and I must admit at the moment im struggling linking my code scripts together. Im used to having a main class to work from.

I have been trying to implement virtual touch joysticks. I have the following code and it does control the player, but it is just moving a rigidbody.

What I want is to make this virtual joystick give out a relevant X,Y vector2 so that I can access it in another class, specifically for this case I am trying to use it with the prefab Car from the Standard Assets pack. All I am trying to do for now is if up is held, accelerate the car.

The car prefab code is not altered so far. And therefore it works if I use keyboard, but if I build for Android the touch joysticks respond to touch but I cant get the car to move.

Here is the joystick code im using:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]

public class PlayerMoveController : MonoBehaviour {

// PUBLIC
public SimpleTouchController leftController;
public SimpleTouchController rightController;
public float speedMovements = 5f;
public float speedContinuousLook = 100f;
public float speedProgressiveLook = 3000f;
public bool continuousRightController = true;

// PRIVATE
private Rigidbody _rigidbody;
private Vector3 localEulRot;
private Vector2 prevRightTouchPos;

void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
rightController.TouchEvent += RightController_TouchEvent;
rightController.TouchStateEvent += RightController_TouchStateEvent;
}

public bool ContinuousRightController
{
set{continuousRightController = value;}
}

void RightController_TouchStateEvent (bool touchPresent)
{
if(!continuousRightController)
{
prevRightTouchPos = Vector2.zero;
}
}

void RightController_TouchEvent (Vector2 value)
{
if(!continuousRightController)
{
Vector2 deltaValues = value - prevRightTouchPos;
prevRightTouchPos = value;

transform.localEulerAngles = new Vector3(transform.localEulerAngles.x - deltaValues.y * Time.deltaTime * speedProgressiveLook,
transform.localEulerAngles.y + deltaValues.x * Time.deltaTime * speedProgressiveLook,
0f);
}
}

void Update()
{
// move
_rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
(transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );

if(continuousRightController)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook,
transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook,
0f);
}
}

void OnDestroy()
{
rightController.TouchEvent -= RightController_TouchEvent;
rightController.TouchStateEvent -= RightController_TouchStateEvent;
}

}


Again, the question is how do I get a public variable Vector2 of the X and Y movement of the joystick, instead of directly moving the rigid body from inside this code.

Thanks for any help.

EDIT: The rigidbody is just in a primitive sphere for my testing purpose, it has nothing to do with the car prefab. I want to change the sphere for a car prefab and using the XY Vector I was talking about above alter the car controller code to take those values and accelerate, brake etc.

• This should be as simple as exposing a public Vector2 field/property/getter method and providing a reference to this input script to the gameplay script that needs to query it. Classic methods are assigning the reference through an Inspector variable, passing it to an initialization method when spawning the player, finding it through a search using eg. FindObjectOfType or exposing it as a static getter or singleton. What have you tried so far? Do you need specific help getting any of those options to work? – DMGregory Jul 9 '17 at 3:20
• Thank you for your speedy answer. I often have to wait a while and so have posted this just before i go to sleep :\ typical i get someone willing to help immediately this time :) .. to answer ur questions, ive tried a few things, but im really struggling with how to layout my project, like normally I have a main class, then I can easily reference things and pass references about but for some reason in unity i cant seem find visible references to the variables – Big T Larrity Jul 9 '17 at 3:24
• I realise i can make it static and i have achieved this in another simple project following a tutorial, but my previous learning in other languages I was always told to avoid using static variables everywhere – Big T Larrity Jul 9 '17 at 3:26
• There's nothing stopping you from making a main class that coordinates your other classes, and that can be queried by them to share information. We can't make those architectural decisions for you though, so you'll have to pick a method you want to use, implement it, and let us know if you run into any snags making it work. – DMGregory Jul 9 '17 at 3:28
• but normally my main class is made for me and calls render() each frame and I go from there, it seems illogical to just make one if it not needed. – Big T Larrity Jul 9 '17 at 3:29