Steer Wheel Script

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;
		}
	}
}

Hi, welcome to the forum!

The transform.Rotate function rotates the object relative to its existing rotation so with the code as it is currently, the rotateAngle variable is actually controlling the speed of rotation, not the amount. You can use Quaternion.AngleAxis to create an absolute rotation and then assign it to transform.rotation or transform.localRotation to reorient the steering wheel. If I understand what you are doing correctly, you could use code something like this:-

transform.rotation = Quaternion.AngleAxis(rotateAngle, transform.up);

Thanx a lot andeeee!

This actually cleared out some stuff! And the limitations finaly work! But now when i push play, (i think) it adapts the rotation of my MainCamera (with smoothfollow), and on first touch it rotates a bit by X axis… Even though the objects rotation is zero on start.
Another thing is that the steerwheel Y rotation, bounces when i get close to its rotation limit. I think i’m very close to the result i’m hoping for.

I’m posting the corrected script:

function Update() {
		
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)){
		transform.rotation = Quaternion.identity;
			minAngle = drags.position.x;
			Debug.Log("touched" + minAngle);
		}
		else if(drags.phase == TouchPhase.Moved  Boundry.Contains(drags.position)){
			
			transform.rotation = Quaternion.AngleAxis(rotateAngle * Time.deltaTime * 40, Vector3.up);
			
			maxAngle = drags.position.x;
		}
		
        else if(drags.phase == TouchPhase.Ended){
        	
        	transform.rotation = Quaternion.identity;
			minAngle=maxAngle;
			print("END");

The corrected script does not set rotateAngle which is what I am looking for.