- Home /
rotating a tank turret while the tank is rotating
Hi, im new to unity and i need help.
I want to rotate a turret of a hetzer (just google it, its a tank) The turret is fixed at the front of the tank and can only move there (just google hetzer to see what i mean).
when the tank is not mooving, its working fine, but when i rotate the tank at the same time as i rotate the turret, then the turret rotates out the the boundaries, here is the script:
public var gunPrefab:Transform;
private var gun:Transform;
function Start () {
gun = Instantiate(gunPrefab,transform.position,transform.rotation);
gun.parent = transform;
}
function Update () {
var temp : Vector3;
temp.x = Input.GetAxis("Mouse Y") * (-1) ;
temp.y = Input.GetAxis("Mouse X") ;
temp.z = transform.rotation.z;
if((temp.x > 0 && gun.rotation.x > 0.1) || (temp.x < 0 && gun.rotation.x < (-0.1)))
{
temp.x = 0;
}
if((temp.y > 0 && gun.rotation.y > 0.15) || (temp.y < 0 && gun.rotation.y < (-0.15)))
{
temp.y = 0;
}
gun.transform.Rotate(temp);
}
Answer by M364M4Ncro · Jun 09, 2012 at 10:15 AM
i fixed the problem myself, instead of
if((temp.x > 0 && gun.rotation.x > 0.1) || (temp.x < 0 && gun.rotation.x < (-0.1)))
{
temp.x = 0;
}
if((temp.y > 0 && gun.rotation.y > 0.15) || (temp.y < 0 && gun.rotation.y < (-0.15)))
{
temp.y = 0;
}
i used
if((temp.x > 0 && gun.localRotation.x > 0.1) || (temp.x < 0 && gun.localRotation.x < (-0.1)))
{
temp.x = 0;
}
if((temp.y > 0 && gun.localRotation.y > 0.15) || (temp.y < 0 && gun.localRotation.y < (-0.15)))
{
temp.y = 0;
}
Answer by rubenpvargas · Jun 08, 2012 at 07:59 PM
i get the same problem (i tried this: gun.transform.RotateAround(gun.parent.transform.position, Vector3.up, temp.y 100 Time.deltaTime);)
Answer by Pabong · Oct 30, 2012 at 11:56 AM
3D Motorcycle scene and animation http://www.youtube.com/watch?v=Swm8Qa_NAVI
http://u3d.as/content/pa-bong/future-army-tanks/3qA
Your answer
Follow this Question
Related Questions
Lock rotation of object 4 Answers
tank-tower rotation works unexpected 2 Answers
Rotate Bullet with rotating Turret PROPERLY 2 Answers
How can I rotate a tank's turret back to the starting turret position? 2 Answers
firing a shell from a tank turret 1 Answer