- Home /
Rotating Tank Turret with correct angle
I have a newbie question
I have a simple tank with a turret on it. I need the turret to point a location pointed by mouse and I've managed to do it with code below:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if(Ground.Raycast(ray, out hit, 1000))
{
position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
position.y = 0;
}
transform.LookAt(position);
transform.Rotate(Vector3.down, 90f);
The turret is child of the Tank object.
My problem is when the body of the Tank has an angle to the ground.
Any advice will be appreciated.
You don't actually say what the problem is... What do you want to happen?
To be more specific, I need the turret to continue to show the direction of the mouse pointer (on XZ axis) but to stay correctly on the tank's body. Please check the screenshot to see the problem of the turret.
Hey did you ever figure this out? I'm just trying to find an answer to the same question and it's doing my head in!
Answer by Mischa · Jul 07, 2015 at 05:08 PM
Try to set the rotation of your guntower directly. The Quaternion class offers a LookRotation where you can specify an up vector. Something like this should do the trick:
turretTransform.Rotation = Quaternion.LookRotation(lookPosition - tankPosition, tankUpVector);
Here's the documentation: http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
Thanks for the feedback. I've tried this and some of LookRotation's variations but it didn't work (or maybe I couldn't manage). The Y value raycasted from the ground is not useful to use a lookat function because body of the tank is not parallel to the ground.
Answer by Torigas · Jul 08, 2015 at 09:01 AM
I fear you may have to split the rotation in two parts.
First, the turret itself can rotate just around one axis (pretty much transform.up on the turret). Second, the gun can turn around another axis (transform.right).
That means that you have to split the rotation into different components.
You could, for example, use Quaternion.eulerangles. That gives you the pitch, yaw, roll axis.
Using the code from Mischa's answer:
Transform turret;
Transform gun;
Vector3 direction = position - transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
Vector3 eulers = rotation.eulerAngles;
turret.Rotate(turret.up,eulers.y);
gun.Rotate(gun.right,eulers.x);
Something along those lines might work.
Nice solution but didnt match to my current situation.
In my current scene the tank structure is as below;
->Body -->EmptyGameObject --->Turret ---->Gun (the thin and long cylinder)
I dont need the gun part to be controlled separately.
I raycast the mouse click on the ground plane which gives me a point on XZ plane and when I use the lookat function to this point (which is my target) there is always a angle problem on turret when the tank body is not paralel to the ground (see the screenshot in the question). I need a correct rotation script for my empty game object or for a local rotation of my turret part to fix the issue.
Then just take the turret part of the script I suggested?
Answer by L_Artista · Jan 06, 2016 at 09:33 AM
Vector3 aimVector = playerPosition - transform.position;
aimVector.y = 0.0f;
Quaternion newRotation = Quaternion.LookRotation(aimVector, transform.up);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotateSpeed);
Considering it's a turrent which can only rotate in one axis that will be y! So rotate the turret in y axis only!
Follow this rts tutorial and most of your upcoming problems will be solved! http://stormtek.geek.nz/rts_tutorial/