Hi Everyone. This should not be as hard as it is but I’m just trying to limit the up/down motion of the barrel of the tank. Here’s a video that hopefully illustrates the problem.
I’m just using Transform.Rotate (Vector3. up and Vector3.Right for the turret base and barrel’s rotation since they are two separate objects and it seems to work fine but I just cannot get the clamping to work on the up/down motion when using the transform.Rotate(Vector3.Right. It’s funny that you can make the tank look like it’s on a pogo stick but not optimal for making the barrel behave realistically.
Any suggestions would be appreciated.
Here’s the relevant code…
Thanks!
public class TurretRotate_SciFiTank : MonoBehaviour
{
//Rotating Key Variables
public GameObject sciFiTurretBase; //reference to the turret base of the cannon for side to side movement
public GameObject sciFiBarrel; //reference to the actual barrel of the cannon for up/down movement
public string turretLeft; //key to rotate turret to left
public string turretRight; //key to rotate turret to Right
public string turretUp; //key to rotate turret barrel Up
public string turretDown; //key to rotate turret barrel Down
public Quaternion barrelRotation; //quaternion to hold angle of barrel
public float barrelRotX;
public Quaternion baseRotation;
public float baseRotY;
//public float maxBarrelRot;
//public float minBarrelRot;
public Transform sciFiShotPoint; //position in space to instantiate energy weapon of cannon
//public GameObject sciFiWeapon; //reference to the weapon prefab
public string shootKey; //reference to the shoot key of the weapon.
public GameObject plasmaProjectile; //projectile of sciFi Tank
//public GameObject plasmaMuzzleFlash; //muzzle flash effect for plasma
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//CANNON MOVEMENT INPUTS
if (Input.GetKey(turretRight))
{
//baseRotY += Time.deltaTime * 100f;
sciFiTurretBase.transform.Rotate (Vector3.up * 50f * Time.deltaTime);
}
if (Input.GetKey(turretLeft))
{
//baseRotY -= Time.deltaTime * 100f;
sciFiTurretBase.transform.Rotate (Vector3.up * -50f * Time.deltaTime);
}
if (Input.GetKey(turretUp))
{
//barrelRotX += Time.deltaTime * 100;
sciFiBarrel.transform.Rotate (Vector3.right * 40f * Time.deltaTime);
}
if (Input.GetKey(turretDown))
{
//barrelRotX -= Time.deltaTime * 100;
sciFiBarrel.transform.Rotate (Vector3.right *-40f * Time.deltaTime);
}