I’ve been over this for over some time now, and i can’t get it to match my wishes. I tried practically everything: OnGui did his job well but i need multitouch for it to make it function with my game. I also tried Texture2D but i couldn’t make it work as well. Finally i made a plane and stitched my “steer wheel” texture to it, and parented the plane to my mainCam with smooth follow. This solution actually gave pretty good results but it still has glitches that i can’t seem to fix, so please guys help!.
The idea is this:
I have a steer wheel (plane-texure), that is supposed to rotate when i drag only on the X axis. I made a boundary around the touch position so the script only works when i touch inside. On TouchPhase.Begin it should identify the starting position and put a default of “0” rotation on Y axis, when i drag to the left - Steerwheel rotates to -Y, and to the right - Steerwheel rotates to +Y.
I managed to get this to work but i can’t seem to manage to limit the Y rotation. For example :
When TouchPhase.Begin, Steerwheel Y rotate is 0. When i get to TouchPhase.Moved - to the edge of the +X drag boundary the Y should be the Max +Y rotation and stop until i hold my finger. When i release my finger (TouchPhase.Ended) on the current position, the Steerwheel Y Rotation should return to his default 0.
In my case, the steerwheel either stays on current rotation or sometimes even glitches and Rotates on other Axises(X,Z). I just want it to follow my finger… Sometimes it even act like a swipe instead of a drag an keeps rotating like mad, until i tap once inside the boundary.
I hope you get the idea!
This is the script:
var RotateTime: float = 0.8;
var SwipeTime: float = 0.4;
var rotateAngle : float = 0.0;
var minAngle;
var maxAngle;
var AnglePos : float = 10;
var AngleNeg : float = -10;
function Update() {
transform.Rotate(0, rotateAngle * Time.deltaTime * 50, 0);
var Boundry : Rect = Rect(0, 0, 250, 250);
for (var j=0; j < Input.touchCount; ++j){
var drags : Touch = Input.touches[j];
if(drags.phase == TouchPhase.Began Boundry.Contains(drags.position)){
minAngle = drags.position.x;
Debug.Log("touched" + minAngle);
}
else if(drags.phase == TouchPhase.Moved Boundry.Contains(drags.position)){
maxAngle = drags.position.x;
}
else if(drags.phase == TouchPhase.Ended){
minAngle=maxAngle;
rotateAngle = 0;
}
if(minAngle!=maxAngle){
rotateAngle = (maxAngle - minAngle)/RotateTime;
}
else{
rotateAngle = rotateAngle * SwipeTime;
}
if (rotateAngle >= AnglePos){
rotateAngle = AnglePos;
minAngle=maxAngle;
}
else if (rotateAngle <= AngleNeg){
rotateAngle = AngleNeg;
minAngle=maxAngle;
}
}
}