# Need help with rotation

 0 Hi, Im writing a simple tank game, It has a tank with a turret and a cannon attached and a hilly terrain. The problem is that when the tank is at an angle the script doesnt rotate the turret and the cannon correctly. The turret house should only rotate on Z and the cannon attached on X. I think the problem is that my script doesnt take into account the new angle the tanks is in. Can anybody help with this problem? Thanks. The following script controls the turret. ``````var TargetLoc : Vector3; var targetObject : GameObject; var RotateSpeed = 6.0; function Update () { if(targetObject !=null ) TargetLoc= targetObject.transform.position; // Rotate towards target var TankRotation = transform.rotation; var targetRotation = Quaternion.LookRotation (TargetLoc - transform.position); transform.rotation = Quaternion.Slerp(TankRotation, targetRotation, Time.deltaTime * RotateSpeed); transform.localRotation.y = 0; transform.localRotation.x = 0; var targetRotation2 = Quaternion.LookRotation (TargetLoc - transform.Find("TurretHouse/Cannon").position); transform.Find("TurretHouse/Cannon").rotation = Quaternion.Slerp(transform.Find("TurretHouse/Cannon").rotation, targetRotation2, Time.deltaTime * RotateSpeed); transform.Find("TurretHouse/Cannon").localRotation.z =0; transform.Find("TurretHouse/Cannon").localRotation.y =0; } `````` more ▼ asked Apr 28 '10 at 10:51 PM Tryggvi 11 ● 1 ● 1 ● 1 Eric5h5 80.3k ● 42 ● 132 ● 521 I think Ive realized the problem. It has to do with the change in x axis when importing from blender. http://answers.unity3d.com/questions/6635/asset-import-from-3dsmax-rotation-issue Apr 29 '10 at 04:47 PM Tryggvi add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

4 answers: sort voted first
 0 just another suggestion, I would make sure that the script is operating in local space not world space. When in local space, the script should ignore the world space rotation of your tank. more ▼ answered Jun 10 '10 at 07:23 PM n8 192 ● 11 ● 14 ● 26 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Have a look at this question, which goes into detail on locking rotation and that kind of thing. It also has a good follow script, just make sure your player is tagged "Player". more ▼ answered Apr 28 '10 at 11:08 PM e.bonneville 5.7k ● 100 ● 116 ● 165 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 You could also separate the turret as a game object and the tank as another game object and reprogram 2 scripts one for tank and one for turret. And possibly use input functions to control the tank and turret as u see fit. more ▼ answered Apr 29 '10 at 12:43 AM khalil 38 ● 6 ● 7 ● 12 The Tank and the turret has its own script. The tank is controled my the keypad, and the turret locks on to a target via mouse. Maybe I didnt explain my problem correctly. The turret and cannon rotate, on a level surface, correctly. It aims the cannon at its target. But when the tank goes up a hill or is leaning sideways, the aim is of. The turret doesnt rotate correctly. It appears to not rotate enough... Apr 29 '10 at 09:26 AM Tryggvi add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 I think Ive realized the problem. It has to do with the change in x axis when importing from blender. http://answers.unity3d.com/questions/6635/ Tryggvi 17 mins ago more ▼ answered Apr 29 '10 at 05:06 PM Tryggvi 11 ● 1 ● 1 ● 1 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Topics:

x2166
x577
x440
x39

asked: Apr 28 '10 at 10:51 PM

Seen: 2445 times

Last Updated: Apr 29 '10 at 12:17 AM