- Home /
TurretRotation on a starship (full 3d)
I have a starship that moves in full 3d (up down, left right, yaw, pitch roll thrust)...And it has turrets on it...How do i limit the turret body to just turn in around a local Y-axis, and the barrel would move separetly around local x axis... here is my code, and if you have the time how do i limit the barrel rotation? Thanks
var LookAtTarget:Transform; var damp = 1.0;
function Update () { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); transform.localRotation = Quaternion.Slerp(transform.localRotation, rotate, Time.deltaTime * damp); } }
well i just saw that my script is not working fine :(...
Answer by Paulius-Liekis · Sep 28, 2010 at 10:33 AM
Look at this project: http://unity3d.com/support/resources/example-projects/3rdpersonshooter
It has exactly what you're looking for: flying object which turns on Y axis and rotates turret on X axis.
Answer by privateknez · Sep 28, 2010 at 07:27 PM
Thats in C# :(. Thats a lot of code and im a begginer...But thanx anyway. But i will explain more in detail what i am trying to do.... i have a ship in space and it can move and rotate in all axis.. and it has turrets on ot (something like homeworld). i have a camera that rotates around the ship (mouse orbit), and i placed an empty object as a camera child an moved it far away along the z axis (this is the target), so when i move my camera around the ship all turrets point that way. it works with one code just fine but the turrets remain in local space(they dont follow the rotation of the ship)... and here is my problem...
Answer by privateknez · Sep 28, 2010 at 11:32 PM
I think i got it :)
ill put the code, i didnt make it... i found it here on the forums it was in C# i just made it for java
this is for the turret body
var Target:Transform; var SwivelSpeed = 1.0;
function Update () {
if(Target) { var localTarget: Vector3 = transform.InverseTransformDirection(Target.transform.position - transform.position); var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) Mathf.Rad2Deg; var adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime SwivelSpeed); transform.Rotate(Vector3.up, adjustedAngle); } }
and this for the barrel
var Target:Transform; var SwivelSpeed = 1.0;
function Update () {
if(Target) { var localTarget: Vector3 = transform.InverseTransformDirection(Target.transform.position - transform.position); var targetAngle = Mathf.Atan2(localTarget.z, localTarget.y) Mathf.Rad2Deg; var targetAngle2 = targetAngle - 90; var adjustedAngle = Mathf.Lerp(0, targetAngle2, Time.deltaTime SwivelSpeed); transform.Rotate(Vector3.right, adjustedAngle ); } }
So this turret will follow target when tank, ship or whatever moves in 3d space. now i just have to merge it in one code and restrict the barrel angle...
This was very useful to me. Tried using other people's signed angle functions and quaternions (I actually got Quaternions to work, but couldn't lock them to a single axis of rotation). Thanks again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074123im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fireing arc 1 Answer
Need help with turret targeting system (rotation issues) 4 Answers
Play Sound when turret rotates 1 Answer
2d turret problem 1 Answer
Simple LookAt Rotation with offset pivot 2 Answers