Hey guys. I’m new with Unity3d and i have some really necessary questions. I develop a game for Android devices now and i have come to the point, when i need to create a touch controls. I have read a lot about it, but i still can’t understand. What I should do:
-
when hold a GUITexture button, animation is playing. And when I do not hold it - it doesn’t do anything.
-
I’ve used this tutorial to create a moveable vehicle. It works good, but I used a keyboard keys to drive. Now i need to change it for touch.
Maybe it seems a bit silly, but can i do this: when I touch a GUITexture, engine thinks, that I touch PC keyboard key and do what it should do. I can not change the basic code of a car - i just create a virtual GUITexture key, which is a “link” to the real PC keyboard key, which I don’t have on Android (ah, it’s pretty obvious
)
If textwall above doesn’t make sense, please describe me how to create a basic touch operations with Unity. I’ve already seen a Penelope tutorial, but there are a lot of unnecessary information for me on this stage.
Thanks in advance 
maybe post your current controller code and then someone could let you know at what stage to change it.
Jason
I spend 1 week for understand how touch really works and develop my own touch system but unfortunately its uses some external sprite plugin’s textures since Gui textures much expensive. Play with Penelope touch little bit more u will crack soon. and post ur code too
Unity3D support for Android and the touch system is a joke. it is very bad and it hard to read how to use it.
I had the same problem and i manage to fix it, but I did not think it was a good way.
But then I did find Playmaker.
With it the touch part is very easy and now I do not spend much time with it.
I just set up GUI text and controller etc in minutes and it works.
I also find many good things in Playmaker.
I think Unity3D should buy Playmaker and make it standard in Unity3D. most things that is hard to do in Unity3D is very easy with Playmaker.
I also find some bugs in Unity3D GUI system. Thats why I dis start using Playmaker in the first place.
Here is a code from this tutorial. I need to adaptate its controls to an Android Touch. it will be great, if you help me.
public var brakeLights : Material;
var throttle : float = 0;
private var steer : float = 0;
// These variables allow the script to power the wheels of the car.
var FrontLeftWheel : WheelCollider;
var FrontRightWheel : WheelCollider;
// These variables are for the gears, the array is the list of ratios. The script
// uses the defined gear ratios to determine how much torque to apply to the wheels.
var GearRatio : float[];
var CurrentGear : int = 0;
// These variables are just for applying torque to the wheels and shifting gears.
// using the defined Max and Min Engine RPM, the script can determine what gear the
// car needs to be in.
var EngineTorque : float = 600.0;
var MaxEngineRPM : float = 500.0;
var MinEngineRPM : float = 100.0;
private var EngineRPM : float = 0.0;
function Start () {
// I usually alter the center of mass to make the car more stable. I'ts less likely to flip this way.
rigidbody.centerOfMass.y = -1.5;
}
function Update () {
// This is to limith the maximum speed of the car, adjusting the drag probably isn't the best way of doing it,
// but it's easy, and it doesn't interfere with the physics processing.
rigidbody.drag = rigidbody.velocity.magnitude / 40;
// Compute the engine RPM based on the average RPM of the two wheels, then call the shift gear function
EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
// set the audio pitch to the percentage of RPM to the maximum RPM plus one, this makes the sound play
// up to twice it's pitch, where it will suddenly drop when it switches gears.
audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0 ;
// this line is just to ensure that the pitch does not reach a value higher than is desired.
if ( audio.pitch > 1.3 ) {
audio.pitch = 1.3;
}
// finally, apply the values to the wheels. The torque applied is divided by the current gear, and
// multiplied by the user input variable.
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
// the steer angle is an arbitrary value multiplied by the user input.
FrontLeftWheel.steerAngle = 26 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 26 * Input.GetAxis("Horizontal");
}
function ShiftGears() {
// this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
// the engine RPM fall within the desired range. The gear is then set to this "appropriate" value.
if ( EngineRPM >= MaxEngineRPM ) {
var AppropriateGear : int = CurrentGear;
for ( var i = 0; i < GearRatio.length; i ++ ) {
if ( FrontLeftWheel.rpm * GearRatio[i] < MaxEngineRPM ) {
AppropriateGear = i;
break;
}
}
CurrentGear = AppropriateGear;
}
if ( EngineRPM <= MinEngineRPM ) {
AppropriateGear = CurrentGear;
for ( var j = GearRatio.length-1; j >= 0; j -- ) {
if ( FrontLeftWheel.rpm * GearRatio[j] > MinEngineRPM ) {
AppropriateGear = j;
break;
}
}
CurrentGear = AppropriateGear;
}
}
I removed all code that queried the TouchPhase and implemented something similiar myself. Now it works better. I mostly had problems after the Application woke up from being paused.
Something similar? What exactly?
I’d be interested to know what also, I find the Unity TouchPhase system to slow to respond
Basically I mark the UI as dirty when something happened and remove the dirty flag when Input.touchCount gets zero again. Sometimes I have to store the position of the touch in a global variable to check if the same object has been touched in the beginning and in the end of a touch.