Rotating to touch position

HI,

I have a problem which i can’t seem to solve. I’m trying to rotate a turret to the position where i touch the screen.
So i have the touch position which is a Vector2. And i have to rotate the turret on the Z axis.
Below is a pic of what i mean.
1254044--54526--$98zmli.jpg

Thx!

nop doesn’t work this way…

Attach a 3D plane front of your camera.

Check out the documentation to the following function, you should find a few answers :slight_smile:

  • camera.ScreenPointToRay(vector2)

I’m using easytouch which already has a gesture.position function which returns a Vector2 with the touch position. I only want to know how to rotate the turret. I’m guessing i have to do some cosinus / sinus calculation or something.

Quaternion.LookRotation does the job. You could use ScreenToWorldPoint to get the correct positon.
Now you can use the delta vector between those two in LookRotation.

Could you explain a bit more on how to use this function? I read the scripting reference, but i still don’t know how to add it to the rotation.

EDIT:
Like I said I already got the correct touch position from EasyTouch. The only thing i need to know is how to rotate the turret to the correct angle.

Quaternion.LookRotation kind of translates a vector to a quaternion rotation which looks into the same direction as the vector you provided.
Means, you compute the delta of the turret and the touch position and feed this into the function and apply the result to the turrets rotation.

Now it will look towards the touch position.

Right so what i did:
private Quaternion qat;
qat.SetLookRotation(gesture.position.x, gesture.position.y, 0));

When i debug that i get:
(-0.2, 0.7, 0.2, 0.7)

How does that translate to the rotation which i need to apply to the Z axis of the turret?

You could also use Transform.LookAt.

I tried that but that rotates the whole Cannon whereas i only need to rotate the Z axis…

Edit:
I forgot that EasyTouch also has a function called TouchToWorldPoint which returns a vector of the touch on the screen. Maybe that’s some sort of help?

You can save the rotation of the cannon before you rotate it. After rotation, just apply the saved x and y values back to the cannon.

I know that, the only problem is I get an X and Y value whereas I need to rotate the Z axis. So when I apply transform.rotate which value should I apply to the Z axis?

Edit:
1255067--54655--$Pic.png

So this picture should make clear that i want to calculate the angle between the touch position and side A. What i did (and doesn’t work) is AA + SS = B*B then Sqrt(B) which should give me the correct float for B. Then i did the Cos(A / B) which should give me the angle of A and B but still that gives me something like 0.34f which can’t be right…

Shouldnt it be ‘radian = Acos(A / B)’ ?

However, yes, at first you should convert the screen pos to world pos. Then you can use one of the mothods described in this thread.

My approach in detail:

Transform turretTube = ...; //The tubes transform
Vector3 touchWorldPos = ...; //The touch postion in world coordinates
Vector3 tubePos = turretTube.position; //The tubes world position

Vector3 direction = touchWorldPos - tubePos;
direction.z = 0;//Depending on which plane you set up your game. This should work for x-y plane

Quaternion lookRotation = Quaternion.LookRotation(direction);
turretTube.rotation = lookRotation;

This should work … i hope :stuck_out_tongue:

Quick´n´dirty:

	private float zAngle = 0.0f;
	
	void Update () 
	{
		Vector3 oldAngles = transform.eulerAngles; // save angles
		Vector3 upAxis = new Vector3(0,0,1); // rotation axis
		Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z)); // get world coordinates of mouse position

		transform.LookAt(point, upAxis); // rotate the transform around Z axis		
		zAngle = -transform.eulerAngles.z; // save angle of Z axis - negative or positive depends on your camera view direction		
		transform.eulerAngles = new Vector3(oldAngles.x, oldAngles.y, zAngle);	// reset rotations, except on Z axis	
	}

Thx fogman for the code works for some part. The turret now rotates but only between -270, 0, 90, and only those three values. So it’s not rotating to a point say 75 degrees. I have these debugs:
First touch:
Old angles: (0.0, 0.0, 0.0);
point(0.9, 0.0, 0.0);
zAxis: 0.6584662
zAngles: -90.00001

Second touch
Old angle: (0.0, 0.0, 270.0)
point: (-1.0, -0.5, 0.0)
zAxis: 0.5945116
zAngle: -270

Tried some other stuff:

Vector3 oldAngles = Cannon.transform.eulerAngles; // save angles
		Debug.Log("Old angles: " + oldAngles);

        Vector3 point = gesture.GetTouchToWordlPoint(Cannon.transform.position.z); // get world coordinates of mouse position
		
		float x = point.x;			//The X position in worldPoint
		float y = point.y;			//The Y position in worldPoint
		
		calc =Mathf.Sqrt((x*x) + (y*y));		//Calculate B (see image)
		
		float angle = Mathf.Acos(y / calc);		//Calculate Angle in Rad
		float angleZ = angle * Mathf.Rad2Deg;	//Set Rad to degress
		
		point.z = angleZ;						//Set the calculated angle to point
		
        Cannon.transform.LookAt(point, new Vector3(0,0,1)); // rotate the transform around Z axis   
		Debug.Log ("zAxis: " + Cannon.transform.rotation.z);

        zAngle = -Cannon.transform.eulerAngles.z; // save angle of Z axis - negative or positive depends on your camera view direction  
		Debug.Log ("zAngle: " + zAngle);

        Cannon.transform.eulerAngles = new Vector3(oldAngles.x, oldAngles.y, zAngle);  // reset rotations, except on Z axis

I tried calculating the angle with Mathf.ACos(), that gives me some sort of angle but isn’t right when applying it to the point.z vector3.

Edit:
I found out that gesture.GetTouchToWorldPos(0); returns these values on touch somewhere on the screen:
tap1: 0.4, 0.4, 0.0
tap2:-0.9, 0.1, 0.0
tap3: (0.7, -0.8, 0.0)

I don’t know if it’s possible to calculate angles with these values because they are so low.

1 Like

Right i got it working sort of… the code:

Vector3 oldAngles = Cannon.transform.eulerAngles;
		Vector3 upAxis = new Vector3(0,0,1);
		Vector3 point = gesture.GetTouchToWordlPoint(0);
		Debug.Log (point.x);
		
		calc = (point.x * point.x) + (point.y * point.y);
		
		calc = Mathf.Sqrt(calc);
		
		float angleZ = Mathf.Acos(point.y / calc);
		Debug.Log ("z: " + angleZ);
		
		angleZ = angleZ * Mathf.Rad2Deg;
		
		Debug.Log ("deg: " + angleZ);
		
		
		Debug.Log (point.x);
		
		Cannon.transform.LookAt(point, upAxis);
		//angleZ = -Cannon.transform.eulerAngles.z;
		
		if(point.x < -0.0)
		{
			Cannon.transform.eulerAngles = new Vector3(oldAngles.x, oldAngles.y, angleZ);	
		}
		else
		{
			Cannon.transform.eulerAngles = new Vector3(oldAngles.x, oldAngles.y, -angleZ);	
		}

The only thing is it’s now not rotating correctly. It’s rotating to the touch position but it seems like the angle just isn’t right :frowning: