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);
}
}
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...
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...