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.
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.
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?
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:
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…
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;
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.